日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

解决.NET CF 3.5 Bitmap(Stream)未处理异常问题

發布時間:2024/8/22 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 解决.NET CF 3.5 Bitmap(Stream)未处理异常问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

VS2008開發環境,在進行.NET CF 3.5下的WM Socket編程時

Bitmap bmp = new Bitmap(Stream)處出現未處理異常

?(圖片引自:http://www.cnblogs.com/fsyiyun/archive/2011/03/30/1999672.html)?

?

通過摸索,我發覺無論對對象進行內存回收,這個問題依然存在。

究其原因,個人猜測是Bitmap實例化時本身就會打開MemoryStream流,所以再用MemoryStream就很容易內存泄露

因此強列建議,當和Bitmap一起合用時,用?FileStream或其它變通方法替代MemoryStream并及時內存回收

以下是使用參考示例:

主要功能是:拍照上傳。

View Code public class PhotoHelper
{
BaseForm _form; //拍照上傳窗體
TextBox _txtFileName;?//顯示照片路徑
PictureBox _picPhoto;?//顯示照片縮略圖

public PhotoHelper(BaseForm form,TextBox txtFileName,PictureBox picPhoto)
{
_form = form;
_txtFileName = txtFileName;
_picPhoto = picPhoto;
_picPhoto.Image = new Bitmap(300, 350);

}

#region 拍照邏輯
public void TakePhoto()
{
//拍照-----------------------------
CameraCaptureDialog ccd = new CameraCaptureDialog();
ccd.Mode = CameraCaptureMode.Still;
ccd.Owner = _form;
ccd.Title = "拍照";
ccd.Resolution = new Size(1600, 1200);
ccd.StillQuality = CameraCaptureStillQuality.Normal;

if (ccd.ShowDialog() == DialogResult.OK)
{
//將拍好的以縮略圖顯示
ImageChangedArgs args = new ImageChangedArgs(ccd.FileName);
using (Graphics g = Graphics.FromImage(_picPhoto.Image))
{

g.DrawImage(args.Image,
new Rectangle(0, 0, _picPhoto.Image.Width, _picPhoto.Image.Height),
new Rectangle(0, 0, args.Image.Width, args.Image.Height),
GraphicsUnit.Pixel);

}
args.Dispose();
args = null;

_txtFileName.Text = ccd.FileName;


}
ccd.Dispose();
ccd = null;
}
#endregion #region 照片上傳邏輯

public void PhotoSave()
{
if (string.IsNullOrEmpty(_txtFileName.Text))
{
Global.ShowMsg("無照片可上傳");
return;
}

string prompt = "確認上傳照片?";
if (Global.ShowYesNoMsg(prompt) == DialogResult.No)
return;


//將照片轉換為byte[]
Byte[] b = FileToBytes(_txtFileName.Text);

#region todo 將Byte[] 通過webservice上傳服務器,在此請加入自己代碼
// CommandAndTaskIdObj commandObj = _form.CommandAndTaskIdObj;
//WsRmsg msg = Global.GetService().UpFile(commandObj.UserName, commandObj.CommandId ,commandObj.TaskId , b, _txtFileName.Text);
//if (msg.Flag == 0)
//{
// b = null;
// throw new Exception(msg.Emsg);
//}
#endregion

Global.ShowMsg("照片上傳成功");


_txtFileName.Text = string.Empty;
b = null;

}
#endregion

static byte[] FileToBytes(string sFilePath)
{
FileStream inFile = null;
try
{
byte[] binaryData;
inFile = new FileStream(sFilePath, FileMode.Open, FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
return binaryData;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (inFile != null)
{
inFile.Close();
inFile.Dispose();
}
inFile = null;
}
}


internal void PhotoDispose()
{
throw new NotImplementedException();
}
}

public class ImageChangedArgs : EventArgs, IDisposable
{
private Bitmap image;
/// <summary>
/// used to create the thumbnail
/// </summary>
private Stream stream;
public ImageChangedArgs(string imageFilePath)
{

try
{
this.stream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
this.image = new Bitmap(this.stream);
}
catch(Exception ex)
{
this.image = null;
if (this.stream != null)
{
this.stream.Close();
this.stream = null;
}
}
}

public Bitmap Image
{
get
{
return this.image;
}
}

public Stream Stream
{
get
{
return this.stream;
}
}

#region IDisposable Members

public void Dispose()
{
if (this.image != null)
this.image.Dispose();
this.image = null;
if (this.stream != null)
{
this.stream.Close();
this.stream.Dispose();
}
this.stream = null;
}

#endregion
}

從上個例子可以看出,MemoryStream類基本上被FileStream替代。

當加載大的圖片時,只要內存允許,就不會報Exception異常?

?

這是第二篇博客,如有錯誤請及時指出,歡迎留爪~~

?

?

?

轉載于:https://www.cnblogs.com/lindaWei/archive/2012/01/17/2324523.html

總結

以上是生活随笔為你收集整理的解决.NET CF 3.5 Bitmap(Stream)未处理异常问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。