【问题标题】:set resolution of image loaded on canvas wpf c#设置画布上加载的图像的分辨率 wpf c#
【发布时间】:2019-12-07 18:03:56
【问题描述】:

我正在画布上工作并在其上加载图像。如何将图像的分辨率设置为 640X480 像素? decodepixelheight 和 decodepixelwidth 不起作用。

   ImageBrush brush = new ImageBrush();
        BitmapImage src = new BitmapImage(new Uri(("C:\\Users\\i2v\\Desktop\\GoogleMapTA.jpg"), UriKind.Relative));
        src.DecodePixelHeight = 480;
        src.DecodePixelWidth = 640;
        brush.ImageSource = src;
     //   brush.Stretch = Stretch.None;
        canvas.Background = brush;
        canvas.Height = src.Height;
        canvas.Width = src.Width;

【问题讨论】:

    标签: c# wpf image canvas drawing


    【解决方案1】:

    BitmapImage 实现了System.ComponentModel.ISupportInitialize 接口。这意味着它的属性只能在其BeginInitEndInit 方法的调用之间设置:

    var src = new BitmapImage();
    src.BeginInit();
    src.UriSource = new Uri(@"C:\Users\i2v\Desktop\GoogleMapTA.jpg");
    src.DecodePixelHeight = 480;
    src.DecodePixelWidth = 640;
    src.EndInit();
    
    canvas.Background = new ImageBrush(src);
    

    请注意,您通常不会同时设置DecodePixelWidthDecodePixelHeight,因为这可能会破坏图像的原始纵横比。设置一个或另一个。

    【讨论】:

    • 还要注意C:\...不是一个相对的URI。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2014-10-03
    • 2015-10-20
    • 1970-01-01
    相关资源
    最近更新 更多