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 ?
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.
"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...
zielony
Member
221 Points
410 Posts
resize image
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 ? //resize: double width = picture.Width; double height = picture.Height; double newWidth = picture.Width; double newHeight = picture.Height; double factor; if (width > maxWidthHeight || height > maxWidthHeight) { if (width > height) { factor = maxWidthHeight / width; } else { 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);rtpHarry
All-Star
56620 Points
8958 Posts
Re: resize image
Feb 27, 2010 06:01 PM|LINK
Hey,
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...
zielony
Member
221 Points
410 Posts
Re: resize image
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.
rtpHarry
All-Star
56620 Points
8958 Posts
Re: resize image
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?
zielony
Member
221 Points
410 Posts
Re: resize image
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...
zielony
Member
221 Points
410 Posts
Re: resize image
Feb 27, 2010 09:38 PM|LINK
I know:
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));Vipindas
Contributor
5514 Points
810 Posts
Re: resize image
Mar 01, 2010 04:35 AM|LINK
using System.Drawing;
using System.Drawing.Drawing2D;
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;
}
else
{
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);
thumbnailGraph.DrawImage(image, imageRectangle);
thumbnailBitmap.Save(outputPath, image.RawFormat);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
}
then call resize image function as
ResizeImage(400, File1.FileContent, path);
zautashvili
Member
30 Points
53 Posts
Re: resize image
Apr 16, 2010 10:15 PM|LINK
very helpful, thank you
giorgi
monishafrien...
Member
45 Points
28 Posts
Re: resize image
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.....
zautashvili
Member
30 Points
53 Posts
Re: resize image
Jul 23, 2010 08:13 AM|LINK
hi, could you post your code, please..
thanks