【C# 】Bitmap图像通过内存保存为raw图像

一、读取图像为位图

【C#】 读取图像为位图-CSDN博客

C# 从指定路径读取图片源、Bitmap与ImageSource互转、Bitmap与BitmapImage互转、BitmapImage转为byte[]、图片压缩_c# imagesource bitmap-CSDN博客

二、Bitmap图像通过内存保存为raw图像

在C#中,将Bitmap图像保存为RAW格式通常涉及到直接访问图像的像素数据并将其写入到一个二进制流中,因为.NET Framework或.NET Core/Standard库本身并不直接支持RAW图像格式的保存。下面是一个基本示例,说明如何从Bitmap中提取原始像素数据并保存为简单的RAW格式文件。请注意,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();

三、完整示例

using System;
using System.Drawing;
using System.Drawing.Imaging;

public class BitmapToRaw
{
    public static void SaveBitmapAsRaw(Bitmap bitmap, string fileName)
    {
        // 获取Bitmap的像素格式
        PixelFormat format = bitmap.PixelFormat;

        // 锁定Bitmap以便直接访问其像素数据
        BitmapData data = bitmap.LockBits(
            new Rectangle(0, 0, bitmap.Width, bitmap.Height),
            ImageLockMode.ReadOnly,
            format);

        // 计算每一行像素数据的字节数(注意可能会有字节对齐)
        int bytesPerPixel = Image.GetPixelFormatSize(format) / 8;
        int stride = data.Stride;

        // 创建一个新的字节数组来保存像素数据
        byte[] pixels = new byte[stride * bitmap.Height];

        // 将像素数据复制到新数组中
        Marshal.Copy(data.Scan0, pixels, 0, pixels.Length);

        // 解锁Bitmap
        bitmap.UnlockBits(data);

        // 写入RAW文件
        using (var stream = new FileStream(fileName, FileMode.Create))
        {
            // 直接写入像素数据
            stream.Write(pixels, 0, pixels.Length);
        }
    }

    public static void Main()
    {
        // 示例:从Bitmap对象保存为RAW文件
        Bitmap bitmap = new Bitmap("path_to_your_image.jpg");
        SaveBitmapAsRaw(bitmap, "output.raw");

        Console.WriteLine("Bitmap已保存为RAW格式.");
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangnaisheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值