出现“文件被另一个程序占用”的错误?该怎么修改?

这是程序:
public void takefromsaved(IntPtr pData)
{
unsafe
{
byte* pFirstAdd = (byte*)pData;
byte* pLine = pFirstAdd;
int numb = 0;
int maxnumb = actual_w * actual_h;
picturedata=new int[maxnumb];
double frame;
while (numb < maxnumb)
{
frame = *(pLine + 3 * numb) * 0.299 + *(pLine + 3 * numb + 1) * 0.587 + *(pLine + 3 * numb + 2) * 0.114;
picturedata[numb] = (int)frame;
numb++;
using (StreamWriter objFiledata = new StreamWriter(@"e:\DataSaved.doc"))
{
objFiledata.Write(Convert.ToString(frame));
}  
}

public void SnapPicAndSave()
{
Rectangle rect = new Rectangle(0, 0, width, height);
Bitmap dstBitmap = CreateGrayscaleImage(width, height);//这个函数在后面有定义
BitmapData dstBmData = dstBitmap.LockBits(rect,
ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
System.IntPtr dstPtr = dstBmData.Scan0;
int dst_bytes = dstBmData.Stride * height;
byte[] dstValues = new byte[dst_bytes];
System.Runtime.InteropServices.Marshal.Copy(dstPtr, dstValues, 0, dst_bytes);
int numb = 0;
string strAlbumName;
for (int i = height - 1; i >= 0; i--)
for (int j = 0; j < width; j++)
{
dstValues[i * dstBmData.Stride + j] = (byte)m_takegray.picturedata[numb];
strAlbumName = Convert.ToString(m_takegray.picturedata[numb]);
numb++;
}
System.Runtime.InteropServices.Marshal.Copy(dstValues, 0, dstPtr, dst_bytes);
dstBitmap.UnlockBits(dstBmData);
pictureshowBox.Image = dstBitmap;
}

繁星点点滴滴
浏览 119回答 3
3回答

慕桂英4014372

using (StreamWriter objFiledata = new StreamWriter(@"e:\DataSaved.doc")){while (numb < maxnumb){frame = *(pLine + 3 * numb) * 0.299 + *(pLine + 3 * numb + 1) * 0.587 + *(pLine + 3 * numb + 2) * 0.114;picturedata[numb] = (int)frame;numb++;objFiledata.Write(Convert.ToString(frame));}}建议你这样把while放里面去试试。

青春有我

像这种问题,一般就是对文件记性操作的时候,由于没有close就会产生这种错误,你看看你别的代码里有没有操作文件的代码,例如打开,创建之类的,有的话要注意close,否则就会出现这情况

慕莱坞森

程序中有while循环肯定会循环执行,“文件被另一程序占用”很明显的错误了
打开App,查看更多内容
随时随地看视频慕课网APP