【问题标题】:Trying to combine two byte arrays of image data into one bitmap for WPF button试图将图像数据的两个字节数组组合成一个 WPF 按钮的位图
【发布时间】:2014-08-28 23:45:22
【问题描述】:

首先,我想声明我对 .Net 中的映像命名空间知之甚少。

我有两个字节的图像数据数组,我想将它们合并到 WPF 按钮的位图中。我想在 C# 代码中做这一切都是可能的。背景图片要么是 jpeg 要么是 png,而叠加图绝对是带有透明区域的 png。叠加图像是我想添加到原始图像的“售罄”横幅之一。我有一个函数可以将任一图像转换为 button.background 将处理的图像。

我已经对这两个图像都进行了尝试,并且效果很好。下一部分是由于我对 .Net 中的图像处理不熟悉而让我难过的部分。我已经看到了一些关于 RenderTargetBitmap 的东西,但我无法弄清楚如何将基础/背景位图放入其中以在顶部渲染第二个位图,也许还有另一种方法可以做到这一点。这是我从以前的编码器/项目中继承的函数的副本。

我必须获取字节数组并将其转换为我可以直接分配给 button.background 的东西:

public static BitmapImage ByteArrayToImageBrush(this byte[] imageData)
{
    if (imageData == null || imageData.Length == 0) return null;
    var image = new BitmapImage();
    using (var mem = new MemoryStream(imageData))
    {
        mem.Position = 0;
        image.BeginInit();
        image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.UriSource = null;
        image.StreamSource = mem;
        image.EndInit();
    }
    image.Freeze();
    return image;
}

我已经定义了一个继承自按钮调用 CategoryButton 的新类,它还有其他一些杂项。特性。当我使用以下代码在代码中创建按钮时,我可以成功地将任一图像作为按钮的背景:

CategoryButton catbtn = new CategoryButton();
catbtn.Background = new ImageBrush(ByteArrayToImageBrush(bgImageByteArray);
// * or *
catbtn.Background = new ImageBrush(ByteArrayToImageBrush(overlayImageByteArray);

所以我知道该功能运行良好。但是,如何将字节数组或生成的 ImageBrush 转换为我可以“添加”/合并第二个叠加图像的东西,然后将其转换为可用于按钮背景的格式?我知道这是很多问题,但我很困惑,我在试图弄清楚它时感到沮丧!

【问题讨论】:

    标签: c# wpf image bitmap processing


    【解决方案1】:

    最简单的方法是像这样放置两个堆叠的Image 控件:

    <Grid>
        <Image Name="image" Stretch="None" />
        <Image Name="overlay" Stretch="None" />
    </Grid>
    

    只需将适当的ImageSource 分配给每个控件的Source

    如果你真的需要将两张图片合二为一,你可以这样做:

    private static ImageSource CombineImageWithOverlay(byte[] imageBytes, byte[] overlayBytes)
    {
        var image = GetImageFromBytes(imageBytes);
        var overlay = GetImageFromBytes(overlayBytes);
        var visual = new DrawingVisual();
        using (var context = visual.RenderOpen())
        {
            context.DrawImage(image, new Rect(0, 0, image.PixelWidth, image.PixelHeight));
            context.DrawImage(overlay, new Rect(0, 0, overlay.PixelWidth, overlay.PixelHeight));
        }
        var result = new RenderTargetBitmap(image.PixelWidth, image.PixelHeight, image.DpiX, image.DpiY, PixelFormats.Pbgra32);
        result.Render(visual);
        return result;
    }
    
    private static BitmapImage GetImageFromBytes(byte[] bytes)
    {
        using (var stream = new MemoryStream(bytes))
        {
            var img = new BitmapImage();
            img.BeginInit();
            img.CacheOption = BitmapCacheOption.OnLoad;
            img.StreamSource = stream;
            img.EndInit();
            return img;
        }
    }
    

    【讨论】:

    • 抱歉耽搁了……但有了你给我的东西,我正要弄清楚……它按我需要的方式工作,所以谢谢……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多