Visual Studio .NET - A generic error occurred in GDI+. problem

Asked By Fouad Romieh on 21-Dec-06 03:49 AM

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

try this one

Senthil T. replied to Fouad Romieh on 21-Dec-06 05:20 AM
myBitmapCropped.save(Server.MapPath("images1")+"tre.jpg"), thisformat)

ok thanks i solve it but i still have one problem

Fouad Romieh replied to Senthil T. on 21-Dec-06 06:04 AM

my problem is that i can't replace the old image with the new cropped image if i send it with the same name

Tilte

hamit yıldırım replied to Fouad Romieh on 19-Apr-08 09:18 AM

protected void btnRes7_Click(object sender, EventArgs e)

{

HttpPostedFile myFile = File6.PostedFile;

int nFileLen = myFile.ContentLength;

if (nFileLen == 0)

{

// dosya seçimemiş

return;

}

if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")

{

//"dosya jpg değil";

return;

}

try

{

System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

byte[] myData = new Byte[nFileLen];

_image6 = myData;

myFile.InputStream.Read(myData, 0, nFileLen);

MemoryStream ms = new MemoryStream(myData);

Bitmap bitMap1 = new Bitmap(ms);

System.Drawing.Image img = bitMap1.GetThumbnailImage(50, 70, myCallBack, IntPtr.Zero);

img.Save(Server.MapPath("~/images/map7name"));

Image7.ImageUrl = "~/images/map7name";

bitMap1.Dispose();

}

catch (ArgumentException arEx)

{

System.IO.File.Delete(Server.MapPath("~/images/map7name"));

}

}

ı hope this will be help