wpf - WPF中如何实现Image.Clone()?

标签 wpf image-processing

我从 db 获取字节数组 (byte[]) 并使用以下方法渲染到图像控件中:

    public Image BinaryImageFromByteConverter(byte[] valueImage)
    {
        Image img = new Image();
        byte[] bytes = valueImage as byte[];
        MemoryStream stream = new MemoryStream(bytes);
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();
        img.Source = image;
        img.Height = 240;
        img.Width = 240;
        return img;
    }

现在已经呈现了,我想将 Image.Source 从 Image (Control)“复制”到另一个元素,例如:Paragraph..

paragraph1.Inlines.Add(new InlineUIContainer(ImageOne));

但什么也没有出现,我尝试使用 ImageOne.Source 创建一个新图像,但我刚刚发现这个示例带有 Uri(@"path"),我无法应用此方法,因为我的 BitmapImage 来自 byte[] 类型

Image img = new Image();
img.Source = new BitmapImage(new Uri(@"c:\icons\A.png"));

请帮忙解决这个问题,谢谢!

最佳答案

只需创建一个新的 Image 元素并将其源设置为相同的 BitmapImage:

byte[] imageInfo = File.ReadAllBytes("IMG_0726.JPG");

BitmapImage image;

using (MemoryStream imageStream = new MemoryStream(imageInfo))
{
    image = new BitmapImage();
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.StreamSource = imageStream;
    image.EndInit();
}

this.mainImage.Source = image;
this.secondaryImage.Source = image;

如果您只是将一个来源复制到另一个来源,它也可以工作:

this.mainImage.Source = image;
this.secondaryImage.Source = this.mainImage.Source;

关于wpf - WPF中如何实现Image.Clone()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/798369/

相关文章:

c# - MVVM 混合行为和 RelayCommand 未按预期工作

c# - 为什么我的转换器给出无效的转换错误?

c# - http ://schemas. microsoft.com/winfx/2006/xaml/presentation 定义

C# 库尔德语言本地化

python - 在 python 中画一条线直到掩码 OpenCV 的边缘

c# - 给定一组点查找区域

javascript - HTML5 Canvas 增强图像

wpf - Caliburn.Micro 我应该使用 Screen 还是 Conductor.AllActive 作为我的父 View

python - 从图像中的绝对点创建相对点网格

android - Android 上的图像处理