WPF中Image显示本地图片(转)
private void SetSource(System.Windows.Controls.Image image, string fileName)
{
System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(fileName);
int imageWidth = 0, imageHeight = 0;
InitializeImageSize(sourceImage, image, out imageWidth, out imageHeight);
Bitmap sourceBmp = new Bitmap(sourceImage, imageWidth, imageHeight);
IntPtr hBitmap = sourceBmp.GetHbitmap();
BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
bitmapSource.Freeze();
WriteableBitmap writeableBmp = new WriteableBitmap(bitmapSource);
sourceImage.Dispose();
sourceBmp.Dispose();
image.Source = writeableBmp;
}
/// <summary>
/// Initialize ImageSize.
/// </summary>
/// <param name="sourceImage"></param>
/// <param name="image"></param>
/// <param name="imageWidth"></param>
/// <param name="imageHeight"></param>
private static void InitializeImageSize(System.Drawing.Image sourceImage, System.Windows.Controls.Image image,
out int imageWidth, out int imageHeight)
{
int width = sourceImage.Width;
int height = sourceImage.Height;
float aspect = (float)width / (float)height;
if (image.Height != double.NaN)
{
imageHeight = Convert.ToInt32(image.Height);
imageWidth = Convert.ToInt32(aspect * imageHeight);
}
else if (image.Width != double.NaN)
{
imageWidth = Convert.ToInt32(image.Width);
imageHeight = Convert.ToInt32(image.Width / aspect);
}
else
{
imageHeight = 100;
imageWidth = Convert.ToInt32(aspect * imageHeight);
}
}
调用: SetSource(this.imageCur, “C:\1.png”);
http://www.cnblogs.com/yunyou/archive/2013/01/25/2876054.html
WPF中Image显示本地图片(转)的更多相关文章
- Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/1873 ...
- 在InternetExplorer.Application中显示本地图片
忘记了,喜欢一个人的感觉 Demon's Blog » 程序设计 » 在InternetExplorer.Application中显示本地图片 « 对VBS效率的再思考——处理二进制数据 Wo ...
- Android ImageView显示本地图片
Android ImageView 显示本地图片 布局文件 <?xml version="1.0" encoding="utf-8"?> <R ...
- C#将image中的显示的图片转换成二进制
原文:C#将image中的显示的图片转换成二进制 1.将Image图像文件存入到数据库中 我们知道数据库里的Image类型的数据是"二进制数据",因此必须将图像文件转换成字节数组才 ...
- Android 使用开源库StickyGridHeaders来实现带sections和headers的GridView显示本地图片效果
大家好!过完年回来到现在差不多一个月没写文章了,一是觉得不知道写哪些方面的文章,没有好的题材来写,二是因为自己的一些私事给耽误了,所以过完年的第一篇文章到现在才发表出来,2014年我还是会继续在CSD ...
- Atitit. html 使用js显示本地图片的设计方案.doc
Atitit. html 使用js显示本地图片的设计方案.doc 1. Local mode 是可以的..web模式走有的不能兰.1 2. IE8.0 显示本地图片 img.src=本地图片路径无 ...
- Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案
Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案 1. IE8.0 显示本地图片 img.src=本地图片路径无效的解决方案1 1.1. div来完成 ...
- angular 图片加载失败 情况处理? 如何在ionic中加载本地图片 ?
1.angular 图片加载失败 情况处理 在directive中定义组件,在ng-src错误时,调用err-src app.directive('errSrc',function(){ return ...
- Springboot项目中 前端展示本地图片
Springboot项目中 前端展示本地图片 本文使用的是Springboot官方推荐的thymeleaf(一种页面模板技术) 首先在pom文件加依赖 <dependency> <g ...
随机推荐
- 查询ORACLE存储关联表
SELECT DISTINCT * FROM user_sourceWHERE TYPE = 'PROCEDURE'AND upper(text) LIKE '%PS_KL_ABS_002_DATA% ...
- android--------ListView和ExpandableListView的侧滑删除操作
本案例主要实现了ListView和ExpandableListView的侧滑删除操作功能 效果图: ListView的Adapter类 private class SlideAdapter exten ...
- (转载)-关于sg函数的理解
最近学习了nim博弈,但是始终无法理解sg函数为什么sg[S]=mex(sg[S'] | S->S'),看到一篇博文解释的不错,截取了需要的几章节. 四.Sprague-Grundy数的提出 我 ...
- jQuery旋转插件—rotate-摘自网友
jQuery旋转插件—rotate 时间:2013年01月03日作者:愚人码头查看次数:5,660 views评论次数:6条评论 网上发现一个很有意思的jQuery旋转插件,支持Internet Ex ...
- Java虚拟机结构分析
https://www.cnblogs.com/Eason-S/p/5658188.html https://blog.csdn.net/u013256816/article/details/5148 ...
- 069——VUE中vuex之使用getters高效获取购物车商品总价
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- cas AssertionThreadLocalFilter
AssertionThreadLocalFilter AssertionThreadLocalFilter作用很简单,就是将Assertion绑定到ThreadLocal. ThreadLocal 无 ...
- cas 认证管理器
CAS-默认的认证管理器:AuthenticationManagerImpl <bean id="authenticationManager" class="org ...
- golang优先队列
参考博客:https://studygolang.com/articles/13173 基本类型排序 package main import ( "fmt" "sort& ...
- asp.net MVC html.ActionLink的几种参数格式
一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, ...