not able to save image which is already saved with same name and to the same location
-
hi in web application I have saved one bitmap image to serverpath and once again if i try to save another image with the same name to the same server path i am getting follwing error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. and also if i try to delete that image file with the same path using below code if (System.IO.File.Exists(serpath)) { System.IO.File.Delete(serpath); } it is giving The process cannot access the file serpath because it is being used by another process. How to resolve this.i need to overwrite the image with the same name and to the same location and also delete. how to resolve this. if anybody knows,Please Reply me. thanks in advance.
-
hi in web application I have saved one bitmap image to serverpath and once again if i try to save another image with the same name to the same server path i am getting follwing error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. and also if i try to delete that image file with the same path using below code if (System.IO.File.Exists(serpath)) { System.IO.File.Delete(serpath); } it is giving The process cannot access the file serpath because it is being used by another process. How to resolve this.i need to overwrite the image with the same name and to the same location and also delete. how to resolve this. if anybody knows,Please Reply me. thanks in advance.
First make sure you are disposing the old image instance. If the image was loaded from a file, GDI keeps lock on the file until it is disposed. see : Bitmap and Image constructor dependencies for a way to work around this. see: http://stackoverflow.com/a/13496652[^] for a good explanation of GDI+ memory use.
-
First make sure you are disposing the old image instance. If the image was loaded from a file, GDI keeps lock on the file until it is disposed. see : Bitmap and Image constructor dependencies for a way to work around this. see: http://stackoverflow.com/a/13496652[^] for a good explanation of GDI+ memory use.
I have tried like this copying to new bitmap then disposing old bitmap and then saving new bitmap using below code if (System.IO.File.Exists(serpath)) { System.Drawing.Image myimage1; myimage1 = System.Drawing.Image.FromFile(serpath); System.Drawing.Bitmap source_bitmap = new System.Drawing.Bitmap(myimage1); myimage1.Dispose(); int thumb_width = 400, thumb_height = 100; System.Drawing.Bitmap thumb_bitmap = new System.Drawing.Bitmap(thumb_width, thumb_height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb_bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(System.Drawing.Brushes.White, 0, 0, thumb_width, thumb_height); g.DrawImage(source_bitmap, 0, 0, thumb_width, thumb_height); g.Dispose(); System.Drawing.Imaging.ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1); Params.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); thumb_bitmap.Save(serpath, Info[1], Params); } else { signatureImage.Save(serpath, ImageFormat.Jpeg); } but then also it is giving error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+ and not able to delete that file also. how to do this. Please reply me, thanks in advance.
-
I have tried like this copying to new bitmap then disposing old bitmap and then saving new bitmap using below code if (System.IO.File.Exists(serpath)) { System.Drawing.Image myimage1; myimage1 = System.Drawing.Image.FromFile(serpath); System.Drawing.Bitmap source_bitmap = new System.Drawing.Bitmap(myimage1); myimage1.Dispose(); int thumb_width = 400, thumb_height = 100; System.Drawing.Bitmap thumb_bitmap = new System.Drawing.Bitmap(thumb_width, thumb_height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb_bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(System.Drawing.Brushes.White, 0, 0, thumb_width, thumb_height); g.DrawImage(source_bitmap, 0, 0, thumb_width, thumb_height); g.Dispose(); System.Drawing.Imaging.ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1); Params.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); thumb_bitmap.Save(serpath, Info[1], Params); } else { signatureImage.Save(serpath, ImageFormat.Jpeg); } but then also it is giving error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+ and not able to delete that file also. how to do this. Please reply me, thanks in advance.
I learned something new; the BitMap(Image img) constructor does all of that painting of the source Image to a new BitMap for you. Cool! :-D :-D Now for your issue. Give this a try. I rewrote the logic to get the JPEG encoder assuming that is the one you wanted. If not, it is easy to change and understand now. There should really be some handling in there in the unlikely event that the encoder does not exist. But you can add that easily enough.
if (System.IO.File.Exists(serpath))
{System.Drawing.Image myimage1 = System.Drawing.Image.FromFile(serpath); int thumb\_width = 400, thumb\_height = 100; using (System.Drawing.Bitmap thumb\_bitmap = new System.Drawing.Bitmap(thumb\_width, thumb\_height)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb\_bitmap)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.Clear(Color.White); g.DrawImage(myimage1, 0, 0, thumb\_width, thumb\_height); } myimage1.Dispose(); System.Drawing.Imaging.ImageCodecInfo jpeg = default(System.Drawing.Imaging.ImageCodecInfo); jpeg = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(). Where((System.Drawing.Imaging.ImageCodecInfo enc) => enc.FormatID == System.Drawing.Imaging.ImageFormat.Jpeg.Guid) .FirstOrDefault(); System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1); Params.Param\[0\] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); thumb\_bitmap.Save(serpath, jpeg, Params); }
}
else
{
signatureImage.Save(serpath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
} -
I learned something new; the BitMap(Image img) constructor does all of that painting of the source Image to a new BitMap for you. Cool! :-D :-D Now for your issue. Give this a try. I rewrote the logic to get the JPEG encoder assuming that is the one you wanted. If not, it is easy to change and understand now. There should really be some handling in there in the unlikely event that the encoder does not exist. But you can add that easily enough.
if (System.IO.File.Exists(serpath))
{System.Drawing.Image myimage1 = System.Drawing.Image.FromFile(serpath); int thumb\_width = 400, thumb\_height = 100; using (System.Drawing.Bitmap thumb\_bitmap = new System.Drawing.Bitmap(thumb\_width, thumb\_height)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb\_bitmap)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.Clear(Color.White); g.DrawImage(myimage1, 0, 0, thumb\_width, thumb\_height); } myimage1.Dispose(); System.Drawing.Imaging.ImageCodecInfo jpeg = default(System.Drawing.Imaging.ImageCodecInfo); jpeg = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(). Where((System.Drawing.Imaging.ImageCodecInfo enc) => enc.FormatID == System.Drawing.Imaging.ImageFormat.Jpeg.Guid) .FirstOrDefault(); System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1); Params.Param\[0\] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); thumb\_bitmap.Save(serpath, jpeg, Params); }
}
else
{
signatureImage.Save(serpath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}Actually using this code the blank image is coming. Using my previous code only i am getting updated image to the same location with the same name. Only problem is after sending mail with this image iinked to the html and after html mail delivery, i am not able to again save new updated image with same name and to the same location. How to resolve this. Please reply me. Thanks in advance.
-
hi in web application I have saved one bitmap image to serverpath and once again if i try to save another image with the same name to the same server path i am getting follwing error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. and also if i try to delete that image file with the same path using below code if (System.IO.File.Exists(serpath)) { System.IO.File.Delete(serpath); } it is giving The process cannot access the file serpath because it is being used by another process. How to resolve this.i need to overwrite the image with the same name and to the same location and also delete. how to resolve this. if anybody knows,Please Reply me. thanks in advance.