Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在WPF中实现Image.Clone()?_Wpf_Image Processing - Fatal编程技术网

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

如何在WPF中实现Image.Clone()?,wpf,image-processing,Wpf,Image Processing,我从db中获取字节数组(byte[]),并使用以下方法渲染到图像控件中: public Image BinaryImageFromByteConverter(byte[] valueImage) { Image img = new Image(); byte[] bytes = valueImage as byte[]; MemoryStream stream = new MemoryStream(bytes); B

我从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(控件)复制到另一个元素,例如:段落

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"));

请帮助解决此问题,谢谢

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

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;

无法通过这种方式从图像复制源,必须先断开媒体与第一个的连接,然后再尝试将同一对象附加到第二个。可以使用BitmapCacheOption.OnLoad。它不会锁定文件。我已经对代码进行了测试,它可以正常工作。我不建议在没有对像素格式、宽度、高度和步幅进行(反)序列化的情况下,对来自流/任何内容的图像进行(反)序列化。-如果你使用位图源作为图像,你可以读取缓冲区并从缓冲区创建一个新的位图源。