获取64bpp图像色彩 [英] Get 64bpp image color

查看:433
本文介绍了获取64bpp图像色彩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问我用的BitmapData 64bpp图像的色彩信息。我已经成功地做​​到了这一点为32bpp的图像,但它似乎并不为64bpp的图像容易...

I'd like to access color info of my 64bpp image with BitmapData. I've succesfully done this for 32bpp images, but it doesn't seem to be as easy for 64bpp images...

static void Main(string[] args)
{
    Bitmap bmp;
    using (Stream imageStreamSource = new FileStream(bitmapPath, FileMode.Open, FileAccess.Read, FileShare.Read))   //Should I use "using" here? or do I need to keep the stream open for as long as I use the Bitmap?
    {
        PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        BitmapSource bitmapSource = decoder.Frames[0];
        bmp = _bitmapFromSource(bitmapSource);
    }

    unsafe
    {
        int xIncr = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;      //8
        xIncr /= 2;

        BitmapData bData1 = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

        ushort* bData1Scan0Ptr = (ushort*)bData1.Scan0.ToPointer();
        ushort* nextBase = bData1Scan0Ptr + bData1.Stride;

        for (int y = 0; y < bData1.Height; ++y)
        {
            for (int x = 0; x < bData1.Width; ++x)
            {
                var red = bData1Scan0Ptr[2];
                var green = bData1Scan0Ptr[1];
                var blue = bData1Scan0Ptr[0];

                bData1Scan0Ptr += xIncr;      //xIncr = 4
            }

            bData1Scan0Ptr = nextBase;
            nextBase += bData1.Stride;
        }
    }
}

//http://stackoverflow.com/questions/7276212/reading-preserving-a-pixelformat-format48bpprgb-png-bitmap-in-net
private static Bitmap _bitmapFromSource(BitmapSource bitmapsource)
{
    Bitmap bitmap;
    using (MemoryStream outStream = new MemoryStream())     //Should I use "using" here?
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapsource));
        enc.Save(outStream);
        bitmap = new System.Drawing.Bitmap(outStream);
    }
    return bitmap;
}

<击>对于白色图像(255,255,255)我得到0和32早在2个值...?我不知道这是否是正确的,我只是间preting他们是错的,或者他们只是完全错误的。(不是真的不再提供更新code)

编辑:
随着更新code我得到8192为128值的值为255和1768。这仍然没有多大意义的,我...

With the updated code I'm getting 8192 for a 255 value and 1768 for a 128 value. This still doesn't make much sense to me...

我也=知道如果我要加载图像时,可以使用使用的声明,因为 http://msdn.microsoft.com/en-us/library/z7ha67kw 说,我需要保持开放的流为位图的寿命。它似乎做工精细,现在是这样的,或者是因为我的位图复制到使用的声明外声明的另一个位图?

I was also wondering if I should be using the "using" statement when loading the image, because http://msdn.microsoft.com/en-us/library/z7ha67kw says I need to keep the stream open for the lifetime of the bitmap. It seems to work fine the way it is now, or is that because I copy the bitmap to another bitmap declared outside the "using" statement?

推荐答案

通常情况下,64bpp格式将有16位对每个RGBA的,所以你应该使用 USHORT 而不是字节。例如:

Normally, 64bpp formats will have 16 bits for each of RGBA, so you should really be using ushort rather than byte. For example:

BitmapData bData1 = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

byte* bData1Scan0Ptr = (byte*)bData1.Scan0.ToPointer();
byte* nextBase = bData1Scan0Ptr + bData1.Stride;

for (int y = 0; y < bData1.Height; ++y)
{
    ushort* pRow = (ushort*)bData1Scan0Ptr;

    for (int x = 0; x < bData1.Width; ++x)
    {
        var red = pRow[2];
        var green = pRow[1];
        var blue = pRow[0];

        pRow += 4;
    }

    bData1Scan0Ptr = nextBase;
    nextBase += bData1.Stride;
}

也许尝试这一修改,并用结果发表评论,如果它不解决您的问题,那么,如果有必要,我会更新我的答案。

Perhaps try this modification and comment with the result if it doesn't solve your problem, then I will update my answer if necessary.

这篇关于获取64bpp图像色彩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆