C#从picturebox保存图像(GDI+;中发生一般错误)
首先,我正在检索图像,并用它填充Access数据库中的pictureboxC#从picturebox保存图像(GDI+;中发生一般错误),c#,generics,picturebox,C#,Generics,Picturebox,首先,我正在检索图像,并用它填充Access数据库中的picturebox con.Open(); string sql = "select Foto from TSP_Data where KayitNo=" + sNo; OleDbCommand cmdResim = new OleDbCommand(sql, con); using (OleDbDataReader oku = cmdResim.ExecuteRead
con.Open();
string sql = "select Foto from TSP_Data where KayitNo=" + sNo;
OleDbCommand cmdResim = new OleDbCommand(sql, con);
using (OleDbDataReader oku = cmdResim.ExecuteReader())
{
while (oku.Read())
{
byte[] veri = oku["Foto"] as byte[];
using (MemoryStream mstream = new MemoryStream())
{
mstream.Write(veri, 0, veri.Length);
Image fotop = Image.FromStream(mstream);
digerFormFoto.Image = fotop;
}
}
}
将图像毫无错误地检索到picturebox并没有什么问题,它工作得很好
但当我尝试使用picturebox save方法和savefiledialog保存图像时,会出现如下错误:
GDI+中发生一般错误。
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Images|*.png;*.bmp;*.jpg";
ImageFormat format = ImageFormat.Png;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ext = System.IO.Path.GetExtension(sfd.FileName);
switch (ext)
{
case ".jpg":
format = ImageFormat.Jpeg;
break;
case ".bmp":
format = ImageFormat.Bmp;
break;
case ".png":
format = ImageFormat.Png;
break;
}
digerFormFoto.Image.Save(sfd.FileName, format); //digerFormFoto is picturebox
如果映像已经被GDI+使用,或者由于GDI+在使用时锁定了映像,因此之前未对其进行处理,则可能会发生这种情况。我建议您使用内存流重新创建图像,或者从图片框创建一个新位图,然后将其保存为以下内容:
Bitmap bitmapImage = new Bitmap(digerFormFoto.Image);
bitmapImage.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
如果您尝试在不指定格式的情况下保存图像会怎么样?