zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    tomcat 自动部署代码
    weex Android
    视频大全
    sql语句
    来一个朴素的验证码小插件
    tcp通信客户端本地日志查看
    python练习题
    由count(sno)和count(cno)引发的思考
    centos7命令行和图形界面的相互切换(附centos7安装配置教程)
    Jenkins配置有用摘抄笔记
  • 原文地址:https://www.cnblogs.com/ExMan/p/3755418.html
Copyright © 2011-2022 走看看