首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将图像添加到image.source中

将图像添加到image.source中
EN

Stack Overflow用户
提问于 2016-11-11 04:56:54
回答 3查看 628关注 0票数 0

我正在尝试将图像追加到图像源,但是在执行代码后,图像不会显示在我的页面中。

代码:

代码语言:javascript
复制
 Bitmap bmp = (Bitmap)data.img;
 MemoryStream ms = new MemoryStream();
 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
 ms.Position = 0;
 BitmapImage bi = new BitmapImage();
 bi.BeginInit();
 bi.StreamSource = ms;
 bi.EndInit();
 imgPhoto.Source = bi;

这是我想要附加到data.img的imhPhoto.Source规范。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-13 05:27:33

在研究了这个问题之后,解决这个问题的办法是:

调用函数获取BitmapImage并将其保存在照片变量中,如下所示:

代码语言:javascript
复制
BitmapImage photo = byteToImage(byte[] buffer)

此功能可将字节转换为BitmapImage。

代码语言:javascript
复制
public BitmapImage byteToImage(byte[] buffer)
{

 using(var ms = new MemoryStream(buffer))
 {
   var image = new BitmapImage();
   image.BeginInit();
   image.CacheOption = BitmapCacheOption.OnLoad;
   image.StreamSource =  ms;
   image.EndInit();
 }

 return image;
}

最后,将转换后的照片附加到图像源,如下所示:

代码语言:javascript
复制
imgPhoto.Source = photo;
票数 0
EN

Stack Overflow用户

发布于 2016-11-11 05:59:37

你可以像这样分配路径。

代码语言:javascript
复制
  //for App folder path
            //var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)+"\\Image\\pic.jpg";

            //for machine path
            var path = @"C:\Users\User1\Pictures\pic.jpg";

            Bitmap bmp = new Bitmap(path);
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ms.Position = 0;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();
            imgPhoto.Source = bi;
票数 -2
EN

Stack Overflow用户

发布于 2016-11-11 07:09:04

如果这解决了您的问题:

代码语言:javascript
复制
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(@"g:\screenshot.png");

BitmapImage bi = new BitmapImage();
bi.BeginInit();

MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;            

bi.StreamSource = ms;
bi.EndInit();

imgPhoto.Source = bi;
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40541363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档