我从 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/