如何在WPF中实现Image.Clone()? [英] How achieve Image.Clone() in WPF?

查看:138
本文介绍了如何在WPF中实现Image.Clone()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从db获取一个byte(byte [])数组,并使用以下方法渲染到Image Control中:

I obtain an array of byte (byte[]) from db and render into a Image Control using the following method :

    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 ..

So now that is rendered, I want to "copy" the Image.Source from Image (Control) to another element, for example : Paragraph..

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

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

but nothings appears, I try to create a new Image using ImageOne.Source but I just found this example with Uri(@"path"), I cant apply this method cause my BitmapImage comes from a byte[] type

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

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

Helps with this issue please, thanks!

推荐答案

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

Just create a new Image element and set its source to the same 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;

如果您只将一个来源复制到另一个来源,它也有效:

It also works if you just copy one source to the other:

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

这篇关于如何在WPF中实现Image.Clone()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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