这似乎是整个网络上一个臭名昭着的错误.因为我的情景不适合,所以我无法找到问题的答案.将图像保存到流时会抛出异常.
奇怪的是这与png完美配合,但是jpg和gif给出了上述错误,这是相当混乱的.
最相似的问题涉及将图像保存到没有权限的文件.具有讽刺意味的是,解决方案是使用内存流,因为我正在做....
public static byte[] ConvertImageToByteArray(Image imageToConvert)
{
using (var ms = new MemoryStream())
{
ImageFormat format;
switch (imageToConvert.MimeType())
{
case "image/png":
format = ImageFormat.Png;
break;
case "image/gif":
format = ImageFormat.Gif;
break;
default:
format = ImageFormat.Jpeg;
break;
}
imageToConvert.Save(ms, format);
return ms.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
更多细节到例外.造成这么多问题的原因是缺乏解释:(
System.Runtime.InteropServices.ExternalException was unhandled by user code
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
at Caldoo.Infrastructure.PhotoEditor.ConvertImageToByteArray(Image imageToConvert) in C:\Users\Ian\SVN\Caldoo\Caldoo.Coordinator\PhotoEditor.cs:line 139
at …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
protected void SaveMyImage_Click(object sender, EventArgs e)
{
string imageUrl = Hidden1.Value;
string saveLocation = Server.MapPath("~/PictureUploads/whatever2.png") ;
HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
WebResponse imageResponse = imageRequest.GetResponse();
Stream responseStream = imageResponse.GetResponseStream();
using (BinaryReader br = new BinaryReader(responseStream))
{
imageBytes = br.ReadBytes(500000);
br.Close();
}
responseStream.Close();
imageResponse.Close();
FileStream fs = new FileStream(saveLocation, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
try
{
bw.Write(imageBytes);
}
finally
{
fs.Close();
bw.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
顶部imageUrl声明采用Base64图像字符串,我想将其转换为图像.我认为我的代码集仅适用于像"www.mysite.com/test.jpg"这样的图像而不适用于Base64字符串.有人有什么建议吗?谢谢!
我有以下代码:
MemoryStream foo(){
MemoryStream ms = new MemoryStream();
// write stuff to ms
return ms;
}
void bar(){
MemoryStream ms2 = foo();
// do stuff with ms2
return;
}
Run Code Online (Sandbox Code Playgroud)
我分配的MemoryStream是否有可能以后不能被处理掉?
我有一个同行评审坚持我手动关闭它,我找不到信息来判断他是否有一个有效点.
我正在努力上传并将该图像的缩略图副本保存在缩略图文件夹中.
我正在使用以下链接:
http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx
但
newBMP.Save(directory + "tn_" + filename);
Run Code Online (Sandbox Code Playgroud)
导致异常"GDI +中发生了一般错误."
我试图给予文件夹权限,还试图在保存时使用新的单独的bmp对象.
编辑:
protected void ResizeAndSave(PropBannerImage objPropBannerImage)
{
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(fuImage.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int thumbWidth = 100;
int thumbHeight = thumbWidth / sngRatio;
int bannerWidth = 100;
int bannerHeight = bannerWidth / sngRatio;
// Create a new …Run Code Online (Sandbox Code Playgroud) 我有一个从Panel创建的全局Graphics对象.定期从磁盘中拾取图像并使用Graphics.DrawImage()将其绘制到面板中.它适用于几次迭代,然后我得到以下有用的例外:
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y)
at System.Drawing.Graphics.DrawImage(Image image, Point point)
Run Code Online (Sandbox Code Playgroud)
当我完成它时处理图像对象时,我排除了内存泄漏.我知道图像没有损坏,并且可以正常读取,因为程序在面板停止显示之前执行了一段时间.
我在使用PictureBox时遇到了同样的问题,但这次至少我得到了一个错误而不是什么.
我检查了任务管理器中的GDI对象和USER对象,但是当应用程序正常工作时,它们总是大约有65个用户对象和165个GDI对象.
我确实需要尽快解决这个问题,并不是说我可以在.NET系统库中保留断点并查看确切执行失败的位置.
提前致谢.
编辑:这是显示代码:
private void DrawImage(Image image)
{
Point leftCorner = new Point((this.Bounds.Width / 2) - (image.Width / 2), (this.Bounds.Height / 2) - (image.Height / 2));
_graphics.DrawImage(image, leftCorner);
}
Run Code Online (Sandbox Code Playgroud)
图像加载代码:
private void LoadImage(string filename, ref Image image)
{
MemoryStream memoryStream = DecryptImageBinary(Settings.Default.ImagePath + filename, _cryptPassword);
image = Image.FromStream(memoryStream);
memoryStream.Close();
memoryStream.Dispose();
memoryStream = null; …Run Code Online (Sandbox Code Playgroud) 因此,在发现Bitmap类期望原始流在图像或位图的生命周期内保持打开之后,我决定找出Bitmap类在处理时是否实际关闭了流.
查看源代码,Bitmap和Image类创建一个GPStream实例来包装流,但不存储对GPStream或Stream实例的引用.
num = SafeNativeMethods.Gdip.GdipLoadImageFromStreamICM(new GPStream(stream), out zero);
Run Code Online (Sandbox Code Playgroud)
现在,GPStream类(内部)没有实现Release或Dispose方法 - 什么都不允许GDI关闭或处理流.由于Image/Bitmap类没有保留对GPStream实例的引用,因此GDI,Drawing.Bitmap或Drawing.Stream似乎无法正确关闭流.
我可以继承Bitmap来解决这个问题,但是,等等,它是密封的.
请告诉我,我错了,并且MS不仅无法编写不会泄漏API资源的代码.
我有这个函数在函数中返回一个Image,使用Image.FromStream方法创建图像根据MSDN:
您必须在图像的生命周期内保持流打开
所以我没有关闭流(如果我关闭了蒸汽,则从返回的图像对象中抛出GDI +异常).我的问题是当在返回的Image上的其他地方调用Image.Dispose()时是否关闭/处理流
public static Image GetImage(byte[] buffer, int offset, int count)
{
var memoryStream = new MemoryStream(buffer, offset, count);
return Image.FromStream(memoryStream);
}
Run Code Online (Sandbox Code Playgroud)
正如其中一个答案中所建议的,使用不是要走的路,因为它会引发异常:
public static Image GetImage(byte[] buffer, int offset, int count)
{
using(var memoryStream = new MemoryStream(buffer, offset, count))
{
return Image.FromStream(memoryStream);
}
}
public static void Main()
{
var image = GetImage(args);
image.Save(path); <-- Throws exception
}
Run Code Online (Sandbox Code Playgroud)
我有一个WPF应用程序从视频文件中获取快照图像.用户可以定义从中获取图像的时间戳.然后将图像保存到磁盘上的临时位置,然后将其渲染为<image>元素.
然后,用户应该能够选择不同的时间戳,然后覆盖磁盘上的临时文件 - 然后应该在<image>元素中显示.
使用Image.Source = null;,我可以从元素中清除图像文件<image>,因此它会显示一个空白区域.但是,如果源图像文件随后被新图像(具有相同名称)覆盖并加载到<image>元素中,则它仍然显示旧图像.
我使用以下逻辑:
// Overwrite temporary file file here
// Clear out the reference to the temporary image
Image_Preview.Source = null;
// Load in new image (same source file name)
Image = new BitmapImage();
Image.BeginInit();
Image.CacheOption = BitmapCacheOption.OnLoad;
Image.UriSource = new Uri(file);
Image.EndInit();
Image_Preview.Source = Image;
Run Code Online (Sandbox Code Playgroud)
<image>即使原始文件已完全替换,元素中显示的图像也不会更改.这里是否存在我不知道的图像缓存问题?
我的应用程序列出目录中的所有MP3,当用户选择文件时,它会加载标签信息,包括专辑封面.将该艺术加载到用户保存数据时使用的变量中.该艺术品也被加载到图片框中供用户查看.
// Global to all methods
System.Drawing.Image currentImage = null;
// In method onclick of the listbox showing all mp3's
TagLib.File f = new TagLib.Mpeg.AudioFile(file);
if (f.Tag.Pictures.Length > 0)
{
TagLib.IPicture pic = f.Tag.Pictures[0];
MemoryStream ms = new MemoryStream(pic.Data.Data);
if (ms != null && ms.Length > 4096)
{
currentImage = System.Drawing.Image.FromStream(ms);
// Load thumbnail into PictureBox
AlbumArt.Image = currentImage.GetThumbnailImage(100,100, null, System.IntPtr.Zero);
}
ms.Close();
}
// Method to save album art
TagLib.Picture pic = new TagLib.Picture();
pic.Type = TagLib.PictureType.FrontCover;
pic.MimeType = …Run Code Online (Sandbox Code Playgroud)