首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取BitmapSource的PixelFormat

如何获取BitmapSource的PixelFormat
EN

Stack Overflow用户
提问于 2011-06-16 22:41:44
回答 1查看 4.6K关注 0票数 7

我使用以下代码将BitmapSource转换为Bitmap

代码语言:javascript
复制
internal static Bitmap ConvertBitmapSourceToBitmap(BitmapSource bitmapSrc)
{
    int width = bitmapSrc.PixelWidth;
    int height = bitmapSrc.PixelHeight;
    int stride = width * ((bitmapSrc.Format.BitsPerPixel + 7) / 8);

    byte[] bits = new byte[height * stride];

    bitmapSrc.CopyPixels(bits, stride, 0);

    unsafe
    {
        fixed (byte* pBits = bits)
        {
            IntPtr ptr = new IntPtr(pBits);

            return new System.Drawing.Bitmap(
                width,
                height,
                stride,
                System.Drawing.Imaging.PixelFormat.Format32bppPArgb, //The problem
                ptr);
        }
    }
}

但是我不知道如何获取BitmapSourcePixelFormat,所以我的图像被损坏了。

对于上下文,我使用这种技术是因为我想加载一个tiff,它可能是8或16灰色,或者24或32位颜色,并且我需要保留PixelFormat。我更喜欢修复我的ConvertBitmapSourceToBitmap,因为它相当方便,但也很乐意用从BitmapSource创建位图的更好的技术来替换以下代码:

代码语言:javascript
复制
Byte[] buffer = File.ReadAllBytes(filename.FullName);
using (MemoryStream stream = new MemoryStream(buffer))
{
    TiffBitmapDecoder tbd = new TiffBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

    return BitmapBitmapSourceInterop.ConvertBitmapSourceToBitmap(tbd.Frames[0]);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-17 03:53:09

使用BitmapSource.Format有什么问题吗?这是PixelFormat,您已经在使用它来确定步幅。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6373762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档