[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

asp.net resize uploaded image only when image is bigger than required and remove original image from server

Posted on 2008-11-07
10
Medium Priority
?
230 Views
Last Modified: 2012-05-05

I am using the solution code from this question:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23783127.html

How would I modify the code so that if the original image is smaller than the new required size, the only thing it will do is save the original image but as a "jpg"?
Also it appears that you cannot save the new image as the original images name because I get the error. "A generic error occurred in GDI+".  So, can you also provide code on how to remove the original image from the server? Thank you.
0
Comment
Question by:thefarm
  • 5
  • 5
10 Comments
 
LVL 1

Author Comment

by:thefarm
ID: 22908306
Also, how would I modify the code if I only cared about what the height of the image was, say 300? And don't care about the width, as long as it is proportionate.
0
 
LVL 4

Accepted Solution

by:
BToson earned 2000 total points
ID: 22911041
Something like this should work:
ResizeReplaceImage("Image.jpg", 0, 200)
Sub ResizeReplaceImage(ByVal FileName$, ByVal Width%, ByVal Height%)
	Using Image = Bitmap.FromFile(FileName)
		Dim OriginalFormat = Image.RawFormat
		Dim NewHeight% = If(Height > 0, Height, (Image.Height / Image.Width) * Width)
		Dim NewWidth% = If(Width > 0, Width, (Image.Width / Image.Height) * Height)
		Using NImage = New Bitmap(NewWidth, NewHeight)
			Using NGraphics = Graphics.FromImage(NImage)
				NGraphics.DrawImage(Image, 0, 0, NewWidth, NewHeight)
				NGraphics.Save()
			End Using
			Image.Dispose()
			NImage.Save(FileName, OriginalFormat)
		End Using
	End Using
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:thefarm
ID: 22922279
Not sure what to modify to test the code you provided.  I'm using Visual Studio 2005 and it has underlined about 10 different things in the code.  To include the "if" statements and variables.
0
Independent Software Vendors: 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!

 
LVL 4

Expert Comment

by:BToson
ID: 22922307
Sorry, you did not specify your intended framework target.
Please try replacing them with Iif.
Eg:  Dim NewHeight% = Iif(Height > 0, Height, (Image.Height / Image.Width) * Width)
0
 
LVL 1

Author Comment

by:thefarm
ID: 22923117
That worked.  What would I dimension the variable originalformat as?
0
 
LVL 4

Expert Comment

by:BToson
ID: 22923238
If that doens't work, strip the line and change this:
NImage.Save(FileName, OriginalFormat)
to
NImage.Save(ResizeImage,System.Drawing.Imaging.ImageFormat.Jpeg)
0
 
LVL 4

Expert Comment

by:BToson
ID: 22923250
Apologies, was looking at the wrong code!
NImage.Save(FileName)
0
 
LVL 1

Author Comment

by:thefarm
ID: 22923352
Thanks.
   Sub ResizeReplaceImage(ByVal FileName$, ByVal Width%, ByVal Height%)
        Using Image As Bitmap = Bitmap.FromFile(FileName)
            If Image.Height > Height Then
                Dim OriginalFormat As Imaging.ImageFormat = Image.RawFormat
                Dim NewHeight% = IIf(Height > 0, Height, (Image.Height / Image.Width) * Width)
                Dim NewWidth% = IIf(Width > 0, Width, (Image.Width / Image.Height) * Height)
                Using NImage As Bitmap = New Bitmap(NewWidth, NewHeight)
                    Using NGraphics As Graphics = Graphics.FromImage(NImage)
                        NGraphics.DrawImage(Image, 0, 0, NewWidth, NewHeight)
                        NGraphics.Save()
                    End Using
                    Image.Dispose()
                    NImage.Save(FileName, OriginalFormat)
                End Using
            End If
        End Using
    End Sub

Open in new window

0
 
LVL 4

Expert Comment

by:BToson
ID: 22923367
All working now then?
0
 
LVL 1

Author Comment

by:thefarm
ID: 22923397
All working.  Thanks.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 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