C# Bitmap图像通过内存保存为raw图像
- System.Drawing.Bitmap bitBufferRGB = new System.Drawing.Bitmap("Bitmap.jpg");
- System.Drawing.Imaging.BitmapData data = bitBufferRGB.LockBits(
- new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitBufferRGB.Size),
- System.Drawing.Imaging.ImageLockMode.ReadWrite, bitBufferRGB.PixelFormat);
-
- //获取内存
- IntPtr pData = data.Scan0;
- int width = bitBufferRGB.Width;
- int height = bitBufferRGB.Height;
- if (bitBufferRGB.PixelFormat == PixelFormat.Format8bppIndexed)
- {
- byte[] laterData = new byte[width * height];
- System.Runtime.InteropServices.Marshal.Copy(pData, laterData, 0, width * height);
- File.WriteAllBytes("D:\\DICOM\\" + string.Format("{0:yyyyMMddHHmmssfff}", DateTime.Now) + "image.raw", laterData);
- }
- else if (bitBufferRGB.PixelFormat == PixelFormat.Format24bppRgb)
- {
- byte[] laterData = new byte[width * height * 3]; // 注意这里,我们按照每个像素3字节来计算数组的大小
- System.Runtime.InteropServices.Marshal.Copy(pData, laterData, 0, width * height * 3); // 注意这里,我们按照每个像素3字节来复制数据
- File.WriteAllBytes("D:\\DICOM\\" + string.Format("{0:yyyyMMddHHmmssfff}", DateTime.Now) + "image.raw", laterData);
- }
-
-
- //解锁及释放资源
- bitBufferRGB.UnlockBits(data);
- bitBufferRGB.Dispose();