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

  • 相关阅读:
    c调用python记录
    linux 进程内存基础
    doubango类面向对象研究
    HBuilderX 连接 逍遥模拟器 之 解决没法找到模拟器设备 问题
    Application,Session,Cookie,ViewState和Cache区别
    每个.Net开发人员应该下载的十种必备工具
    ASP.NET面试资料
    .net 主题与样式
    浅谈C#当中的out关键字
    5ResponseModel响应模型
  • 原文地址:https://www.cnblogs.com/ExMan/p/3755418.html
Copyright © 2011-2022 走看看