wince的UnauthorizedAccessException异常如何解决?

流木晴天 2011-06-27 04:03:05
代码如下,我简单模拟连续截图,程序刚开始运行好好的,但是在随机的第N幅的时候会弹出UnauthorizedAccessException异常,字面意思是无存取权限,但目录是有存储权限的,前面N幅都能生成,也没有超过大小限制,我猜想是内存没释放的问题,但是我都加了Dispose()后还是出现同样问题,郁闷


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace DeviceApplication1
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 1000; i++)
{
string fileName = string.Format("SDMMC\\IMAGE\\{0}.jpg",i);
GetImage(fileName);
}
MessageBox.Show("Hello");
}

public void GetImage(string fileName)
{
int iHeight = Screen.PrimaryScreen.Bounds.Height;
int iWidth = Screen.PrimaryScreen.Bounds.Width;
IntPtr dc1 = WINAPI.CreateDC(null, null, null, (IntPtr)null);
Graphics gScreen = Graphics.FromHdc(dc1);
Bitmap iGetPhoto = new Bitmap(
iWidth
, iHeight);
Graphics gPhoto = Graphics.FromImage(iGetPhoto);
IntPtr iHandle = gScreen.GetHdc();
IntPtr iPhoto = gPhoto.GetHdc();

WINAPI.BitBlt(iPhoto, 0, 0, iWidth, iHeight,
iHandle, 0, 0, WINAPI.SRCCOPY);
iGetPhoto.Save(fileName, ImageFormat.Jpeg);
gScreen.ReleaseHdc(iHandle);
gPhoto.ReleaseHdc(iPhoto);
gScreen.Dispose();
gPhoto.Dispose();

iGetPhoto.Dispose();
WINAPI.DeleteDC(dc1);
}
}

public class WINAPI
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter

[System.Runtime.InteropServices.DllImportAttribute("coredll.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, // 驱动名称
string lpszDevice, // 设备名称
string lpszOutput, // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
);

[DllImport("coredll.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("coredll.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("coredll.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("coredll.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("coredll.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("coredll.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
}
...全文
95 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧