我从 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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)