Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ASP.NET - Resize image code help

Posted on 2009-12-24
6
Medium Priority
?
395 Views
Last Modified: 2013-11-07

I'm trying to take a file on the webserver in:
"e:\web\htdocs\profile\myimage.jpg"
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));
}

Open in new window

0
Comment
Question by:FeverCanada
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26122423
what is the problem with ur code?
0
 
LVL 2

Expert Comment

by:entryg
ID: 26123510
You have to explicitly end your response by adding Response.End() after resizing 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

Open in new window

0
 

Author Comment

by:FeverCanada
ID: 26128559
Is it possible to have an on-demand view that doesn't save the thumbnail?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:FeverCanada
ID: 26143852
Hi there, can you show me how to call the sub also...  I'm still a little cloudy on that part.
0
 
LVL 2

Accepted Solution

by:
entryg earned 1500 total points
ID: 26151615
Here is the complete GenerateThumbnail.aspx page listing.
------------------------------------------------------


After you can utilize this code by using

<img src="GenerateThumbnail.aspx?file=abc.jpg" />

Hope this helps.

<%@ Page Language="VB" %>
<script runat="VB">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            GetResizedImage(Request.QueryString(file).ToString())
            Response.End()
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
</script>

Open in new window

0
 

Author Closing Comment

by:FeverCanada
ID: 31669908
Thanks finally got it :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question