创建一个从System.Drawing.Image对象文件系统上的新形象? [英] Create a new image on the file system from System.Drawing.Image?
问题描述
好吧,我很抱歉,这可能是一个小白的问题,但我有点卡住了。
Ok, I'm sorry, this is probably a noob question but I'm kinda stuck.
所以我在做什么(在我的asp.net应用程序)加载从文件系统的图像:
So what I'm doing (on my asp.net application) is loading an image from the file system:
System.Drawing.Image tempImage;
tempImage = System.Drawing.Image.FromFile(HttpContext.Server.MapPath(originalPath));
然后我做了一些调整大小:
Then I do some resizing:
tempImage = my awesomeResizingFunction(tempImage, newSize);
和打算使用此将其保存到文件系统中的另一个位置:
and intend to save it to the file system in another location using this:
string newPath = "/myAwesomePath/newImageName.jpg";
tempImage.Save(newPath);
和我所得到的这个错误是:
and what I get is this error:
"A generic error occurred in GDI+."
我知道图像是OK因为我能写出来给浏览器,看到调整后的图像,当我试图挽救它,我只能得到错误。我有点新卡住了,我在做这完全错了? (嗯,我想这是显而易见的,但你知道我的意思......)
I know the image is "ok" because I can write it out to the browser and see the resized image, I only get the error when I try to save it. I'm kinda new and stuck, am I doing this totally wrong? (Well, i guess that's obvious but you know what I mean...)
推荐答案
PLZ试试这个code ......我用同样的code调整大小的图像和保存。如果你得到的任何问题,那么PLZ让我知道。
plz try this code...I have used the same code for resizing image and saving. if you get any problem then plz let me know.
System.Drawing.Bitmap bmpOut = new System.Drawing.Bitmap(NewWidth, NewHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, NewWidth, NewHeight);
g.DrawImage(new System.Drawing.Bitmap(fupProduct.PostedFile.InputStream), 0, 0, NewWidth, NewHeight);
MemoryStream stream = new MemoryStream();
switch (fupProduct.FileName.Substring(fupProduct.FileName.IndexOf('.') + 1).ToLower())
{
case "jpg":
bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "jpeg":
bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "tiff":
bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Tiff);
break;
case "png":
bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
break;
case "gif":
bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
break;
}
String saveImagePath = Server.MapPath("../") + "Images/Thumbnail/" + fupProduct.FileName.Substring(fupProduct.FileName.IndexOf('.'));
bmpOut.Save(saveImagePath);
其中fupProduct是FileUpload控件ID
where fupProduct is fileupload control ID
这篇关于创建一个从System.Drawing.Image对象文件系统上的新形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!