• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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.
0
thefarm
Asked:
thefarm
  • 5
  • 5
1 Solution
 
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
 
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now