C# GDI+;中发生一般性错误;
我使用以下方法将图像加载到图片框中:C# GDI+;中发生一般性错误;,c#,bitmap,gdi+,C#,Bitmap,Gdi+,我使用以下方法将图像加载到图片框中: picturebox1.Image = Image.FromFile() 我使用以下方法保存它: Bitmap bm = new Bitmap(pictureBox1.Image); bm.Save(FileName, ImageFormat.Bmp); 创建新文件时,它工作得非常好,但当我尝试替换现有映像时,会抛出以下运行时错误: GDI中发生一般性错误+ 那么我能做些什么来解决这个问题呢 由于图像文件由您的picturebox1.image使用,请尝
picturebox1.Image = Image.FromFile()
我使用以下方法保存它:
Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(FileName, ImageFormat.Bmp);
创建新文件时,它工作得非常好,但当我尝试替换现有映像时,会抛出以下运行时错误:
GDI中发生一般性错误+
那么我能做些什么来解决这个问题呢 由于图像文件由您的
picturebox1.image使用,请尝试将其保存到不同的文件路径:
picturebox1.Image = Image.FromFile(FileName);
Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(@"New File Name", ImageFormat.Bmp);
编辑:您还可以首先从图像中添加副本,如:
picturebox1.Image = new Bitmap(Image.FromFile(FileName));
Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(FileName, ImageFormat.Bmp);//no error will occurs here.
试试这个
picturebox1.Image = Image.FromFile(FileName);
Bitmap bm = new Bitmat(pictureBox1.Image);
Image img = (Image)b;
img.Save(FileName, ImageFormat.Bmp);
FromFile
方法锁定文件,因此使用Image.FromStream()方法读取图像:
byte[] bytes = System.IO.File.ReadAllBytes(filename);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
pictureBox1.Image = Image.FromStream(ms);
然后像以前一样保存。如果路径不存在,也会发生这种情况
您可以使用以下命令创建目录:
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(FileName));
从文件构造位图对象或图像对象时,该文件在对象的生存期内保持锁定。因此,您无法更改图像并将其保存回其原始文件
GDI+、JPEG图像到MemoryStream中发生一般错误:
Image.Save(..) // throws a GDI+ exception because the memory stream is closed
编辑:只是从内存中写入。保存到“中间层”MemoryStream
应该可以:
例如,替换此:
Bitmap newBitmap = new Bitmap(thumbBMP);
thumbBMP.Dispose();
thumbBMP = null;
newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg);
比如:
string outputFileName = "...";
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
thumbBMP.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
正如@Jalal Aldeen Saa'd所说,图片框正在使用该文件,并在文件替换时被锁定
//unlock file by clearing it from picture box
if (picturebox1.Image != null)
{
picturebox1.Image.Dispose();
picturebox1.Image = null;
}
//put back the picture inside the pictureBox?
试试这个,会有用的
public void SavePicture()
{
Bitmap bm = new Bitmap(this.myBitmap)
bm.Save("Output\\out.bmp" ,System.Drawing.Imaging.ImageFormat.Bmp );
}
如果忘记添加文件名,也可能发生这种情况:
bm.Save(@"C:\Temp\Download", System.Drawing.Imaging.ImageFormat.Png);
并且可以通过添加文件名来修复:
bm.Save(@"C:\Temp\Download\Image.png", System.Drawing.Imaging.ImageFormat.Png);
注意:实际上,您不必添加扩展即可使其工作。尝试以下方法:
private void LoadPictureBoxWithImage( string ImagePath)
{
Stream objInputImageStream = null;
BitmapData bmdImageData = null;
Bitmap bmpSrcImage = null, bmTemp = null;
byte[] arrImageBytes = null;
int bppModifier = 3;
try
{
objInputImageStream = new MemoryStream();
using (FileStream objFile = new FileStream(ImagePath, FileMode.Open, FileAccess.Read))
{
objFile.CopyTo(objInputImageStream);
}
bmpSrcImage = new Bitmap(objInputImageStream);
bppModifier = bmpSrcImage.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;
//reda from byte[] to bitmap
bmdImageData = bmpSrcImage.LockBits(new Rectangle(0, 0, bmpSrcImage.Width, bmpSrcImage.Height), ImageLockMode.ReadOnly, bmpSrcImage.PixelFormat);
arrImageBytes = new byte[Math.Abs(bmdImageData.Stride) * bmpSrcImage.Height];
System.Runtime.InteropServices.Marshal.Copy(bmdImageData.Scan0, arrImageBytes, 0, arrImageBytes.Length);
bmpSrcImage.UnlockBits(bmdImageData);
pbSetup.Image = (Bitmap)bmpSrcImage.Clone();
pbSetup.Refresh();
}
catch (Exception ex)
{
throw new Exception("Error in Function " + System.Reflection.MethodInfo.GetCurrentMethod().Name + "; " + ex.Message);
}
finally
{
if (objInputImageStream != null)
{
objInputImageStream.Dispose();
objInputImageStream = null;
}
if (bmdImageData != null)
{
bmdImageData = null;
}
if (bmpSrcImage != null)
{
bmpSrcImage.Dispose();
bmpSrcImage = null;
}
if (bmTemp != null)
{
bmTemp.Dispose();
bmTemp = null;
}
if (arrImageBytes != null)
{
arrImageBytes = null;
}
}
}
GDI+中发生一般错误。
我也面临同样的问题。我尝试了很多方法来解决这个问题。最后,我找到了一个我出错的地方。问题是我在文件路径中使用了空格,这是不可接受的。现在,在删除C前面撇号后的空格后,它工作正常:
"SupplyItems":"C:\\inetpub\\HIBMS_Ver1\\BarcodeImages\\Supply\\"
相反。。。我用了一个以下的
"SupplyItems":" C:\\inetpub\\HIBMS_Ver1\\BarcodeImages\\Supply\\"
小错误,但查找和修复它花费了很长时间。请注意,由Image.Clone()创建的图像仍会导致GDI+错误,如下面的错误代码所示,您必须使用Image.FromStream()方法读取图像,如本页上的解决方案所示
谢谢。如果要替换,我不能这样做吗?如果要替换,应先从pictureBox.image
中删除图像,然后替换,然后将其重新添加到pictureBox.image
,你也可以首先在图片框中添加你的图片副本…@Lakshani:别忘了标记正确回答你问题的答案,这样其他人就会知道你的问题是如何解决的。加上1,我想这可能已经解决了我几个月来遇到的一个问题!当然@ScruffyDuck,Image.FromFile方法将打开该图像文件。@Jon nut没有保存方法?@Lakshani,对不起。我不明白你的意思。如果要保存picturebox的图像,请使用-Bitmap bm=new Bitmat(pictureBox1.image);保存(文件名,ImageFormat.Bmp);在尝试使用.save()方法保存文件时,我遇到了相同的错误。请参考我在这里发布的代码:您必须首先定义openfiledialog。然后从文件中读取图像并使用这些代码。这对你有帮助。
//BAD CODE: the image we will try to save AFTER the original image has been cloned and disposed
Image clonedImage;
//load image from file, clone it then dispose
using (var loadedFromDiskImage = Image.FromFile(filePath))
{
clonedImage = (Image) loadedFromDiskImage.Clone();
}
//you might think the new image can be saved given the original is disposed
//but this doesn't seem to be the way Clone() works
//expect GDI+ error in line below:
clonedImage.Save(filePath);