[英]How to re-size an image with asp.net C# without saving it
I use this code to re-size images, but my problem is that I have to save the original picture then re-size that! 我使用此代码来调整图像大小,但是我的问题是我必须保存原始图片,然后再调整大小! How can I re-size a picture without saving it? 如何在不保存的情况下调整图片大小? I want to re-size the picture first and then save it. 我想先重新调整图片大小,然后再保存。
FileUpload1.SaveAs("saveOriginalFileFirstHere");
string thumbpath = "where resized pic should be saved";
MakeThumbnails.makethumb("saveOriginalFileFirstpath", thumbpath);
public static void makethumb(string savedpath, string thumbpath)
{
int resizeToWidth = 200;
int resizeToHeight = 200;
Graphics graphic;
//Image photo; // your uploaded image
Image photo = new Bitmap(savedpath);
// Image photo = new j
Bitmap bmp = new Bitmap(resizeToWidth, resizeToHeight);
graphic = Graphics.FromImage(bmp);
graphic.InterpolationMode = InterpolationMode.Default;
graphic.SmoothingMode = SmoothingMode.Default;
graphic.PixelOffsetMode = PixelOffsetMode.Default;
graphic.CompositingQuality = CompositingQuality.Default;
graphic.DrawImage(photo, 0, 0, resizeToWidth, resizeToHeight);
bmp.Save(thumbpath);
}
Use the InputStream property of the uploaded file instead: 请使用上载文件的InputStream属性:
I have modified your code to do so: 我已修改您的代码来这样做:
EDIT: You really should dispose of your IDisposables, such as your bitmaps and the stream, to avoid memory leakage. 编辑:您确实应该处置IDisposables,例如位图和流,以避免内存泄漏。 I have updated my code, so it will properly dispose these resources after it's done with them. 我已经更新了我的代码,因此在处理完这些资源之后,它将正确处理这些资源。
string thumbpath = "where resized pic should be saved";
MakeThumbnails.makethumb(FileUpload1.InputStream, thumbpath);
public static void makethumb(Stream stream, string thumbpath)
{
int resizeToWidth = 200;
int resizeToHeight = 200;
using (stream)
using (Image photo = new Bitmap(stream))
using (Bitmap bmp = new Bitmap(resizeToWidth, resizeToHeight))
using (Graphics graphic = Graphics.FromImage(bmp))
{
graphic.InterpolationMode = InterpolationMode.Default;
graphic.SmoothingMode = SmoothingMode.Default;
graphic.PixelOffsetMode = PixelOffsetMode.Default;
graphic.CompositingQuality = CompositingQuality.Default;
graphic.DrawImage(photo, 0, 0, resizeToWidth, resizeToHeight);
bmp.Save(thumbpath);
}
}
This should work the way you want it to, if it doesn't, or if anything is unclear, please let me know. 这应该按照您想要的方式工作,如果不是,或者不清楚的话,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.