解决.NET CF 3.5 Bitmap(Stream)未处理异常问题
VS2008開發(fā)環(huán)境,在進(jìn)行.NET CF 3.5下的WM Socket編程時(shí)
Bitmap bmp = new Bitmap(Stream)處出現(xiàn)未處理異常
?(圖片引自:http://www.cnblogs.com/fsyiyun/archive/2011/03/30/1999672.html)?
?
通過(guò)摸索,我發(fā)覺(jué)無(wú)論對(duì)對(duì)象進(jìn)行內(nèi)存回收,這個(gè)問(wèn)題依然存在。
究其原因,個(gè)人猜測(cè)是Bitmap實(shí)例化時(shí)本身就會(huì)打開MemoryStream流,所以再用MemoryStream就很容易內(nèi)存泄露
因此強(qiáng)列建議,當(dāng)和Bitmap一起合用時(shí),用?FileStream或其它變通方法替代MemoryStream,并及時(shí)內(nèi)存回收。
以下是使用參考示例:
主要功能是:拍照上傳。
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("無(wú)照片可上傳");
return;
}
string prompt = "確認(rèn)上傳照片?";
if (Global.ShowYesNoMsg(prompt) == DialogResult.No)
return;
//將照片轉(zhuǎn)換為byte[]
Byte[] b = FileToBytes(_txtFileName.Text);
#region todo 將Byte[] 通過(guò)webservice上傳服務(wù)器,在此請(qǐng)加入自己代碼
// 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
}
從上個(gè)例子可以看出,MemoryStream類基本上被FileStream替代。
當(dāng)加載大的圖片時(shí),只要內(nèi)存允許,就不會(huì)報(bào)Exception異常?
?
這是第二篇博客,如有錯(cuò)誤請(qǐng)及時(shí)指出,歡迎留爪~~
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lindaWei/archive/2012/01/17/2324523.html
總結(jié)
以上是生活随笔為你收集整理的解决.NET CF 3.5 Bitmap(Stream)未处理异常问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OGRE 学习小记 开发环境的配置
- 下一篇: c++头文件包含技巧