我正在处理画布并在其上加载图像。如何将图像的分辨率设置为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
接口。这意味着它的属性只能在其BeginInit
和EndInit
方法的调用之间设置:
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);
请注意,您通常不会同时设置DecodePixelWidth
和DecodePixelHeight
,因为这可能会破坏图像的天然纵横比。设置一个或另一个。