Hi i have this code below to loop through a folder of images to resize and crop each until all they have the same size 200x200 and after this i need to replace the old images with the new one i think if i use the bitmap.save with the same name of image it will replace it but i got this error :
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
on this line :
myBitmapCropped.save(Server.MapPath("images1/test.jpg"), thisformat)
and i notice that the new bitmap that is created is replaced even if the error is fired while the original one not .Here is my code:
Sub Page_Load(sender as Object, e as EventArgs)
Const IMAGE_DIRECTORY as String = "images/"
Const maxWidth as Integer = 200
Const maxHeight as Integer = 200
dim tWidth as Integer
dim tHeight as Integer
Dim pics as ArrayList = new ArrayList()
Dim s as String, html as String
Dim imgHeight, imgWidth as Integer
dim i as integer
For Each s in System.IO.Directory.GetFiles(Server.MapPath(IMAGE_DIRECTORY), "*.jpg")
i = i + 1
'Get information about the image
Dim currentImage as System.Drawing.Image = System.Drawing.Image.FromFile(s)
imgHeight = currentImage.Height
imgWidth = currentImage.Width
if imgWidth > maxWidth then
tWidth = (imgWidth/maxWidth) * 100
end if
if imgHeight > maxHeight then
tHeight = (imgHeight/maxHeight) * 100
end if
if tWidth = nothing then
tWidth = imgWidth
end if
if tHeight = nothing then
tHeight = imgHeight
end if
'Response.Write(tWidth)
'Response.Write(tHeight)
dim thisFormat=currentImage.rawformat
dim imgOutput as New Bitmap(currentImage, tWidth, tHeight)
'Response.Write(imgOutput.height)
dim myBitmapCropped as New Bitmap(maxWidth,maxHeight)
dim myGraphic = Graphics.FromImage(myBitmapCropped)
myGraphic.DrawImage(imgOutput, new Rectangle(0,0,myBitmapCropped.Width,myBitmapCropped.Height),0,25,myBitmapCropped.Width,myBitmapCropped.Height,GraphicsUnit.Pixel)
myBitmapCropped.save(Server.MapPath("images1/tre.jpg"), thisformat)
imgOutput.dispose()
myBitmapCropped.dispose()
Next
End Sub
thanks in advance