FeverCanada
asked on
ASP.NET - Resize image code help
I'm trying to take a file on the webserver in:
"e:\web\htdocs\profile\myi
and have it resized using .NET Native ASP code.
I"m pretty sure this is the proper code, my problem is I dont know how to finish off this file. I need the completed .aspx file to make this function. Should be easy.
// Load Image (any image format!)from file
System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
// Resize Image (and constrain proportions!)
System.Drawing.Size size = CalculateNewWidthAndHeight(img.Width, img.Height);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(80, 80);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), 0, 0, 80, 80);
g.DrawImage(img, (80 - size.Width) / 2, (80 - size.Height) / 2, size.Width, size.Height);
// Output to Disk (and reload from disk)
System.Drawing.Imaging.EncoderParameters p = new System.Drawing.Imaging.EncoderParameters(1);
p.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 90L);
bmp.Save(fileName2, GetEncoderInfo("image/jpeg"), p);
g.Dispose();
bmp.Dispose();
img.Dispose();
img = System.Drawing.Image.FromFile(fileName2);
// Output to browser
Response.ContentType = "image/jpeg";
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
/// <summary>Calculates new height and width based on height and width restrictions.</summary>
private System.Drawing.Size CalculateNewWidthAndHeight(int width, int height) {
float resize = (float)Math.Max(width, height) / 80f; // will constrain to 80x80
return new System.Drawing.Size((int)(width / resize), (int)(height / resize));
}
what is the problem with ur code?
You have to explicitly end your response by adding Response.End() after resizing image.
Below is the complete code listing for resizing the image.
Below is the complete code listing for resizing the image.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetResizedImage(fileName)
Response.End()
End Sub
Private Sub GetResizedImage()
End Sub
Private Sub GetResizedImage(ByVal imageName As String)
Dim filename As String
filename = Server.MapPath(imageName)
Try
oImgOriginal = System.Drawing.Image.FromFile(filename)
'fetch user supplied filename
'maintain aspect ratio and also fit thumbnail into frame
imgWidth = oImgOriginal.Width
'get original image width and height
imgHeight = oImgOriginal.Height
'maintain aspect ratio
If imgWidth > 200 AndAlso imgHeight > 150 Then
widthProportion = (imgWidth / frameWidth)
heightProportion = (imgHeight / frameHeight)
Dim f As Single = Math.Max(widthProportion, heightProportion)
If (f < 1) Then
f = 1
End If
thumbWidth = CType((imgWidth / f), Integer)
thumbHeight = CType((imgHeight / f), Integer)
'generate thumbnail
oImgThumbNail = oImgOriginal.GetThumbnailImage(thumbWidth, thumbHeight, Nothing, pointer)
'Sending Response gif type to the browser.
Response.ContentType = "image/jpg"
oImgThumbNail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Else
oImgThumbNail = oImgOriginal.GetThumbnailImage(imgWidth, imgHeight, Nothing, pointer)
Response.ContentType = "image/jpg"
oImgThumbNail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
'Disposing the objects.
oImgOriginal.Dispose()
oImgThumbNail.Dispose()
Catch e As Exception
Throw e
End Try
End Sub
ASKER
Is it possible to have an on-demand view that doesn't save the thumbnail?
ASKER
Hi there, can you show me how to call the sub also... I'm still a little cloudy on that part.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks finally got it :)