Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to save and overwrite an image

Status
Not open for further replies.

lily1

Programmer
Mar 13, 2004
17
0
0
US
After cropping the image, I want the cropped image to overwrite the old image. I use code:

Private Function Crop(ByVal Source As Bitmap, ByVal x As Int32, ByVal y As Int32, ByVal width As Int32, ByVal height As Int32) As Bitmap
Dim Cropped As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(Cropped)
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
Dim rect As New Rectangle(0, 0, width, height)
g.DrawImage(Source, rect, x, y, width, height, GraphicsUnit.Pixel)
Return Cropped
End Function

i = System.Drawing.Image.FromFile(imagePath)
cropped=Crop(i,x,y,w,h)
i.dispose()
response.contenttype="image/gif"
cropped.save(imagePath,ImageFormat.gif)
cropped.dispose()

I got error: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

But if I change the code to:
imagepath= newImagePath
cropped.save(Server.MapPath(imagepath),ImageFormat.Gif)

This time the cropped image can be save to newImagePath.
How should I do to save the image back to the old image name? Thanks for help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top