在GDI +中Bitmap.Save方法发生一般性错误 [英] A Generic error occured in GDI+ in Bitmap.Save method
问题描述
我工作的上传和缩略图forder该图像的同一个略图副本。
我使用以下链接:
<一个href=\"http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx\">http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx
但
newBMP.Save(目录+tn_+文件名);
是造成异常GDI +中发生一般性错误。
我试图给在文件夹的权限,还试图保存文档时,使用一个新的独立的BMP对象。
编辑:
保护无效ResizeAndSave(PropBannerImage objPropBannerImage)
{
//创建在存储器FileUpload控件的内容的位图
位图originalBMP =新位图(fuImage.FileContent); //计算新的图像尺寸
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; //创建一个新位图,其中将举办previous调整位图
位图thumbBMP =新位图(originalBMP,thumbWidth,thumbHeight);
位图bannerBMP =新位图(originalBMP,bannerWidth,bannerHeight); //创建一个基于新的位图图形
图形oGraphics = Graphics.FromImage(thumbBMP);
//设置新的图形文件的属性
oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; //绘制基础上,调整位图新的图形
oGraphics.DrawImage(originalBMP,0,0,thumbWidth,thumbHeight); 位图newBitmap =新位图(thumbBMP);
thumbBMP.Dispose();
thumbBMP = NULL; //新的图形文件保存到服务器
newBitmap.Save(〜/图片/大拇指/+T+ objPropBannerImage.ImageId,ImageFormat.Jpeg); oGraphics = Graphics.FromImage(bannerBMP);
//设置新的图形文件的属性
oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; //绘制基础上,调整位图新的图形
oGraphics.DrawImage(originalBMP,0,0,bannerWidth,bannerHeight);
//新的图形文件保存到服务器
bannerBMP.Save(〜/图片/+ objPropBannerImage.ImageId +.JPG);
//一旦与位图对象完毕后,我们释放他们。
originalBMP.Dispose(); bannerBMP.Dispose();
oGraphics.Dispose();
}
在任何一个位图对象或Image对象从构造
文件,该文件仍然锁定为对象的生命周期。作为一个
因此,你不能改变的图像,并将其保存回到同一文件
它起源。
http://support.microsoft.com/?id=814675
块引用><一个href=\"http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream/1053123#1053123\">A在GDI + JPEG图像到的MemoryStream
发生一般性错误<一个href=\"http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed/336396#336396\">Image.Save(..)因为内存流关闭抛出GDI +的异常
<一个href=\"http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html\">http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html
编辑:结果
从内存中只是写...
保存到一个'中间人'内存流,应该工作
块引用>例如。试试这个 - 替换
位图newBitmap =新位图(thumbBMP);
thumbBMP.Dispose();
thumbBMP = NULL;
newBitmap.Save(〜/图片/大拇指/+T+ objPropBannerImage.ImageId,ImageFormat.Jpeg);喜欢的东西:
字符串outputFileName =...;
使用(MemoryStream的内存=新的MemoryStream())
{
使用(的FileStream FS =新的FileStream(outputFileName,FileMode.Create,FileAccess.ReadWrite))
{
thumbBMP.Save(存储器,ImageFormat.Jpeg);
字节[]字节= memory.ToArray();
fs.Write(字节,0,bytes.Length);
}
}I am working on to upload and same a thumnail copy of that image in a thumbnail forder.
I am using following link:
http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx
but
newBMP.Save(directory + "tn_" + filename);
is causing exception "A generic error occurred in GDI+."
I have tried to give permission on folder, also tried to use a new separate bmp object when saving.
Edit:
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 bitmap which will hold the previous resized bitmap Bitmap thumbBMP = new Bitmap(originalBMP, thumbWidth, thumbHeight); Bitmap bannerBMP = new Bitmap(originalBMP, bannerWidth, bannerHeight); // Create a graphic based on the new bitmap Graphics oGraphics = Graphics.FromImage(thumbBMP); // Set the properties for the new graphic file oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new graphic based on the resized bitmap oGraphics.DrawImage(originalBMP, 0, 0, thumbWidth, thumbHeight); Bitmap newBitmap = new Bitmap(thumbBMP); thumbBMP.Dispose(); thumbBMP = null; // Save the new graphic file to the server newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg); oGraphics = Graphics.FromImage(bannerBMP); // Set the properties for the new graphic file oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new graphic based on the resized bitmap oGraphics.DrawImage(originalBMP, 0, 0, bannerWidth, bannerHeight); // Save the new graphic file to the server bannerBMP.Save("~/image/" + objPropBannerImage.ImageId + ".jpg"); // Once finished with the bitmap objects, we deallocate them. originalBMP.Dispose(); bannerBMP.Dispose(); oGraphics.Dispose(); }
解决方案When either a Bitmap object or an Image object is constructed from a file, the file remains locked for the lifetime of the object. As a result, you cannot change an image and save it back to the same file where it originated. http://support.microsoft.com/?id=814675
A generic error occurred in GDI+, JPEG Image to MemoryStream
Image.Save(..) throws a GDI+ exception because the memory stream is closed
http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html
EDIT:
just writing from memory...save to an 'intermediary' memory stream, that should work
e.g. try this one - replace
Bitmap newBitmap = new Bitmap(thumbBMP); thumbBMP.Dispose(); thumbBMP = null; newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg);
with something like:
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); } }
这篇关于在GDI +中Bitmap.Save方法发生一般性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!