C# picturebox内存不足
每次我尝试将图片添加到图片框时,都会出现内存不足的情况 我尝试在开始时进行处理,但当我尝试从文件中添加图像时,它仍然会导致内存不足C# picturebox内存不足,c#,out-of-memory,picturebox,C#,Out Of Memory,Picturebox,每次我尝试将图片添加到图片框时,都会出现内存不足的情况 我尝试在开始时进行处理,但当我尝试从文件中添加图像时,它仍然会导致内存不足 public void water(string path,string oupath) { pictureBox1.Image = Image.FromFile(path); Font myfont = new Font("Times New Roman", 12.0f); Graphics g = Graphics.FromImage(
public void water(string path,string oupath)
{
pictureBox1.Image = Image.FromFile(path);
Font myfont = new Font("Times New Roman", 12.0f);
Graphics g = Graphics.FromImage(pictureBox1.Image);
// Calculate the size of the text
string waterString = waterText.Text;
SizeF sz = g.MeasureString(waterString, myfont);
// drawing position (X,Y)
Brush myBrush = new SolidBrush(Color.White);
int X;
int Y;
//Set the drawing position based on the users
//selection of placing the text at the bottom or top of the image
X = (int)(pictureBox1.Image.Width - sz.Width) / 2;
Y = (int)(pictureBox1.Image.Height - sz.Height);
// draw the water mark text
g.DrawString(waterString, myfont, myBrush, new Point(X, Y));
string filename = Path.GetFileName(path);
path = oupath+"/"+filename;
pictureBox1.Image.Save(@"path",ImageFormat.Jpeg);
pictureBox1.Image.Dispose();
}
你处理图像,但你不处理你创建的任何其他一次性对象,
g
,myFont
,我认为myBrush
也是一次性的。另外,为什么你有pictureBox1
,你可以不用它来完成整个方法。虽然这是真的,那些其他OBEJCT非常小,虽然不处理它们会导致gdi处理问题,但这只会在多次调用后发生。如果你想要的只是标准的画笔,为什么不去买呢。一定要学习使用(Graphics g=Graphics.FromOmage(…)等等。另外:由于您试图保存到刚加载的同一图像文件,可能会出现错误的异常。尝试保存到其他名称!我到底抛出了哪一行?好的,这样我就可以添加对象的处置并向保存的文件添加一些文本,但仍然无法解决开头的问题picturebox1。图像给出了输出o在其他任何东西运行之前,f内存异常