设置加载在Canvas WPF C#上的图像的分辨率



我正在处理画布并在其上加载图像。如何将图像的分辨率设置为640x480像素?解解机和解解宽度不起作用。

   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;

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

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

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

最新更新