我想将图像保存到我从数据库获取的图像路径,但是我得到了“Out Of Memory”错误。我正在做错误的以下代码?
cmd.CommandText = "SELECT imageColumn FROM PhotoTable WHERE id= :id";
cmd.ClearParameters();
cmd.Parameters.AddWithValue(":id", Id);
cmd.ExecuteDataReader();
if(cmd.DataReader.Read())
{
var lob = cmd.ExecuteReader().GetOracleLob(0);
var image = new Bitmap(lob);
picturebox1.Image = image;
picturebox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
filePathName="c://image1.jpeg"//save path and file name
var bm = new Bitmap(picturebox1.Image);
bm.Save(filePathName, System.Drawing.Imaging.ImageFormat.Jpeg);
答案 0 :(得分:0)
您正在创建每行Bitmap
。位图必须是Disposed。当你不再使用位图时,你应该处理它。您可以为图片框创建一个位图,并从其上绘制数据库中的位图。
<强>伪:强>
var pictureBoxBitmap = new Bitmap(width, height, ....);
picturebox1.Image = pictureBoxBitmap;
picturebox1.SizeMode = PictureBoxSizeMode.StretchImage;
using(Graphics pictureBoxGraphics = Graphics.FromImage(pictureBoxBitmap))
{
if(cmd.DataReader.Read())
{
var lob = cmd.ExecuteReader().GetOracleLob(0);
using(var image = new Bitmap(lob))
{
pictureBoxGraphics.DrawImage(..... image);
pictureBox1.Refresh();
}
}
}
像这样的东西。