image.save method is not save image GDI+ error occured

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

 string ServerUploadFolder = HttpContext.Current.Server.MapPath("~/Uploads/" + value.PhysicalFileName);                 MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(ServerUploadFolder);                 Image img = Image.FromFile(ServerUploadFolder);                 var bmp = new Bitmap(img.Width, img.Height);  using (Graphics gfx = Graphics.FromImage(bmp))                 {                     gfx.Clear(Color.White);                     gfx.DrawImage(img, 0, 0, img.Width, img.Height);                 }                 bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);  string imagePath = value.FileUrl.Replace(Path.GetFileName(value.FileUrl), Path.GetFileName(value.FileUrl));                 bmp.Save(imagePath,img.RawFormat); 

In this Save Method There are Problem Occured GDI+ Error . I Have Tried Many Solutions but that is not worked. I am rotate image with 90's degree and after it is rotate it is save in database.

回答1:

In may case the image file already exists in system drive, so app throws the error "A Generic error occured in GDI+".

  1. Verify that the folder must already exist where image is to be saved.
  2. Verify that the file must not exist in the path with the same name.


回答2:

The GDI+ error you're receiving is generic and could refer to any one of a number of different issues. Try the following and see how you get on:

If your image is based on a stream, ensure that it is still open when you do the Save operation. Also, make sure permissions are correct on the destination folder, i.e. that Write permission is enabled.

Finally, here's some example code which worked for me:

// Path to source file string inputPath = Server.MapPath(@"~\Content\somefile.jpg");  // Create an image from the file using (Image img = Bitmap.FromFile(inputPath)) {     // Rotate the image 90 degrees     img.RotateFlip(RotateFlipType.Rotate90FlipNone);     // Path to destination file     string outputPath = Server.MapPath(@"~\Content\rotated" +         System.IO.Path.GetExtension(inputPath));     // Delete the destination file if it already exists     if (System.IO.File.Exists(outputPath))     {         System.IO.File.Delete(outputPath);     }     // Save the rotated image in the same ImageFormat as the source     img.Save(outputPath, img.RawFormat); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!