English 中文(简体)
如何在C#中将8位灰度图像转换为byte[]数组?
原标题:How can I convert an 8-bit grayscale image into a byte[] array in C#?

如何将8位灰度图像的数据放入字节数组?我尝试了以下代码:

private byte[] loadBitmap(string filename, int width, int height)
{
    byte[] data = new byte[width * height];
    BitmapData bmpData = null;
    Bitmap slice = new Bitmap(filename);
    Rectangle rect = new Rectangle(0, 0, slice.Width, slice.Height);
    bmpData = slice.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
    int size = bmpData.Height * bmpData.Stride;
    System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size);
    slice.UnlockBits(bmpData);
    return data;
}

但是,由于Format8pppIndexed,数据数组的结果有一些错误。有什么想法吗?

问题回答

我不知道这是否能帮到你,但我会尽力的。

byte[] buffer = null;
System.Drawing.Image newImg = img.GetThumbnailImage(width, height, null, new System.IntPtr());
using (MemoryStream ms = new MemoryStream()) {
    newImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
buffer = ms.ToArray();

这只是我已经做的事情的一部分。

希望有帮助

在我的应用程序中,我需要为每个像素提供一个表示0-255灰度值的字节。

我已经用图像清晰度

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

private static byte[] GetImageData(byte[] imageData)
{
    using (var image = Image.Load<Rgba32>(imageData))
    {
        var buffer = new byte[image.Width * image.Height];
        var index = 0;

        image.ProcessPixelRows(accessor =>
        {
            for (int y = 0; y < accessor.Height; y++)
            {
                Span<Rgba32> pixelRow = accessor.GetRowSpan(y);
                for (int x = 0; x < pixelRow.Length; x++)
                {
                    ref Rgba32 pixel = ref pixelRow[x];
                    buffer[index] = (byte)((pixel.R + pixel.G + pixel.B) / 3);
                    index++;
                }
            }
        });

        return buffer;
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签