总结:C# Bitmap保存的问题,eg:GDI+中发生一般性错误 .jpg 正由另一个进程使用 ,该进程无法访问文件
string path1 = "E:\1.jpg"; string path2 = "E:\1.bmp"; Bitmap bmp1 = new Bitmap(path1); bmp1.Save(path2,ImageFormat.Bmp);
path2保存的是24位的bmp文件,但是path1 = "E:\1.gif"; 即原来为gif文件,则path2保存的是32位的bmp文件
解决gif保存为24位bmp文件的方案:
Bitmap bmp1 = new Bitmap(path1); bmp1.PixelFormat = PixelFormat.Format24bppRgb; Rectangle cloneRect = new Rectangle(0, 0, bmp1.Width, bmp1.Height); PixelFormat format =PixelFormat.Format24bppRgb; Bitmap cloneBitmap = bmp1.Clone(cloneRect, format); cloneBitmap.Save(path2, ImageFormat.Bmp);
但是可能会出现一些错误:
1. GDI+中发生一般性错误
2. 1.jpg 正由另一个进程使用 ,该进程无法访问文件
解决方案:
FileStream fs = new FileStream(path1, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] buffer = new byte[fs.Length]; int length = 0; int ibyteRead = 0; do { length = fs.Read(buffer, ibyteRead, buffer.Length - ibyteRead); ibyteRead += length; } while (length > 0); MemoryStream mfs = new MemoryStream(buffer); fs.Close(); fs.Dispose(); Image bmp1 = Image.FromStream(mfs); bmp1.Save(path2, ImageFormat.Bmp); bmp1.Dispose(); mfs.Close(); mfs.Dispose();
参考:http://believehaveoneday.blog.163.com/blog/static/12073745220104179658918/
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。