Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 02, 2012 04:46 PM by SohailShaikh
Feb 27, 2010 05:23 PM|LINK
I have created upload image, save it, resize it and save with different name. But in that case I have always 2 images - normal and small. I want only 1 image - small - resize on the fly. So what should I write inside System.Drawing.Image.FromFile() - previous
I have there path to saved image with normal size but now I don't have saved image with normal size so what should be there ?
HttpPostedFileBase file = Request.Files["file"];
System.Drawing.Image picture = System.Drawing.Image.FromFile( ?????????? ); //what should be here ?
double width = picture.Width;
double height = picture.Height;
double newWidth = picture.Width;
double newHeight = picture.Height;
if (width > maxWidthHeight || height > maxWidthHeight)
if (width > height)
factor = maxWidthHeight / width;
factor = maxWidthHeight / height;
newWidth = Math.Round(width * factor, MidpointRounding.AwayFromZero);
newHeight = Math.Round(height * factor, MidpointRounding.AwayFromZero);
Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
Graphics g = Graphics.FromImage(result);
g.DrawImage(picture, 0, 0, (int)newWidth, (int)newHeight);
result.Save(katalog + nowa_nazwa_pliku);
Feb 27, 2010 06:01 PM|LINK
You can create a httphandler to resize stored images on the fly.
Rick Strahl has some nice resize code that I have used recently in a project:
His code is good but for some reason he uses an actual aspx page rather than a asmx httphandler. Its easily transferred over though...
Feb 27, 2010 06:44 PM|LINK
I only want to know what I should write inside System.Drawing.Image.FromFile() ?
In PHP uploaded files before save them have temporary localization which we can check using: $_FILES['file']['tmp_name'] - I need something like that in ASP.NET - I think I should write inside
System.Drawing.Image.FromFile() path to temporary file.
Feb 27, 2010 08:00 PM|LINK
Oh ok sorry.
I dont think there is a tmp location available but you can get the contents uploaded before you write it to file using the .FileBytes property:
I dont think the FromFile() will work with that but is there one that takes a stream of some kind?
Feb 27, 2010 08:26 PM|LINK
"is there one that takes a stream of some kind" - I don't understand what You mean. So I should use System.Drawing.Image.FromStream() - not FromFile but FromStream ? I don't know...
Feb 27, 2010 09:38 PM|LINK
int imageLength = file.ContentLength;
byte binaryImagefile = new byte[imageLength];
file.InputStream.Read(binaryImagefile, 0, imageLength);
System.Drawing.Image picture = System.Drawing.Image.FromStream(new System.IO.MemoryStream(binaryImagefile));
Mar 01, 2010 04:35 AM|LINK
public void ResizeStream(int imageSize, Stream filePath, string outputPath)
var image = Image.FromStream(filePath);
int thumbnailSize = imageSize;
int newWidth, newHeight;
if (image.Width > image.Height)
newWidth = thumbnailSize;
newHeight = image.Height * thumbnailSize / image.Width;
newWidth = image.Width * thumbnailSize / image.Height;
newHeight = thumbnailSize;
var thumbnailBitmap = new Bitmap(newWidth, newHeight);
var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
then call resize image function as
ResizeImage(400, File1.FileContent, path);
Apr 16, 2010 10:15 PM|LINK
very helpful, thank you
Jul 20, 2010 09:22 AM|LINK
hii...vipin ...thnks bt im gettin this two type of errors....
Error 14 The best overloaded method match for 'System.Drawing.Graphics.FromImage(System.Drawing.Image)' has some invalid arguments
Error 13 The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)
plz help me out.....
Jul 23, 2010 08:13 AM|LINK
hi, could you post your code, please..