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


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.
LVL 1
thefarmAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thefarmAuthor Commented:
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
BTosonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thefarmAuthor Commented:
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
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

BTosonCommented:
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
thefarmAuthor Commented:
That worked.  What would I dimension the variable originalformat as?
0
BTosonCommented:
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
BTosonCommented:
Apologies, was looking at the wrong code!
NImage.Save(FileName)
0
thefarmAuthor Commented:
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
BTosonCommented:
All working now then?
0
thefarmAuthor Commented:
All working.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.