本文介绍了从内存中卸载位图时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好.我正在创建一个小的操作图像软件.
问题是,当进程结束时,内存不会卸载,随后程序崩溃.
Hello everyone. I''m creating a little manipulation image software.
The problem is, when the process is ended, the memory doesn''t unload, and later the program crashes.
public Image WatermarkImages(Image ImagetoWatermark, Image Watermark)
{
Bitmap imgBack = new Bitmap(ImagetoWatermark);
Bitmap imgWaterMark = new Bitmap(Watermark);
Graphics imgBackGraph = Graphics.FromImage(imgBack);
Point PointImagePos = WatermarkImagePosition(imgBack.Height, imgBack.Width, imgWaterMark.Height, imgWaterMark.Width);
Rectangle WaterMarkRectangle = new Rectangle(PointImagePos, new Size(imgWaterMark.Width, imgWaterMark.Height));
imgBackGraph.DrawImage(imgWaterMark, WaterMarkRectangle, 0, 0, imgWaterMark.Width, imgWaterMark.Height, GraphicsUnit.Pixel);
Image ImgtoReturn = imgBack;
return ImgtoReturn;
}
private void WatermarkImages
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
FileInfo filename = new FileInfo(listBox1.Items[i].ToString());
string ImageFileName = filename.Name;
Image Img = WaterImage.WatermarkImages(Image.FromFile(filename.FullName), Image.FromFile(WatermarkImagePath));
Img.Save(folderBrowserDialog1.SelectedPath + "\\" + ImageFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
Img.Dispose();
}
}
我该如何修复?
谢谢
How I can repair this?
Thanks
推荐答案
public Image WatermarkImages(Image ImagetoWatermark, Image Watermark)
{
Bitmap imgBack = new Bitmap(ImagetoWatermark);
//No need to create watermark image again
//Bitmap imgWaterMark = new Bitmap(Watermark);
//Again use using to create and dispose graphics object
using (Graphics imgBackGraph = Graphics.FromImage(imgBack))
{
Point PointImagePos = WatermarkImagePosition(imgBack.Height, imgBack.Width, imgWaterMark.Height, imgWaterMark.Width);
Rectangle WaterMarkRectangle = new Rectangle(PointImagePos, new Size(imgWaterMark.Width, imgWaterMark.Height));
imgBackGraph.DrawImage(imgWaterMark, WaterMarkRectangle, 0, 0, imgWaterMark.Width, imgWaterMark.Height, GraphicsUnit.Pixel);
}
// No need to create a handle again. Just return it
//Image ImgtoReturn = imgBack;
return imgBack;
}
private void WatermarkImages()
{
//No need to recreate image from file again and again as you are using the same image as watermark
Image WMImage = Image.FromFile(WatermarkImagePath);
for (int i = 0; i < listBox1.Items.Count; i++)
{
FileInfo filename = new FileInfo(listBox1.Items[i].ToString());
string ImageFileName = filename.Name;
//using will definetly dispose off. so no need to explicitly call dispose
using (Image Img)
{
Img = WaterImage.WatermarkImages(Image.FromFile(filename.FullName), WMImage);
Img.Save(folderBrowserDialog1.SelectedPath + "\\" + ImageFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
Img.Dispose();
}
}
}
这篇关于从内存中卸载位图时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!