在WPF中将放大/缩小应用于原始图像
本文关键字:缩小 应用于 原始 图像 放大 WPF | 更新日期: 2023-09-27 17:58:22
我用这个代码创建了一个图像
OpenFileDialog dlg = new OpenFileDialog();
dlg.FileName = ""; // Default file name
myImage = new Image();
try
{
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string sUri = @dlg.FileName;
Uri src = new Uri(sUri, UriKind.RelativeOrAbsolute);
BitmapImage bmp = new BitmapImage(src);
myImage.Source = bmp;
myImage.Width = 100;
myImage.Height = 100;
}
}
我可以放大/缩小这个图像
public void Scale(int i, Point center)
{
Matrix m = myImage.RenderTransform.Value;
if (i > 0)
m.ScaleAtPrepend(1.1, 1.1, center.X, center.Y);
else
m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, center.X, center.Y);
myImage.RenderTransform = new MatrixTransform(m);
}
但当我得到ActualWidth或Width或ActualHeight/Height时,会返回数字100。这意味着这些更改未应用于原始图像(myImage)。现在如何将更改(缩放或任何更改)应用于原始图像?
Tanx all;
转换只影响应用到的对象的外观/布局,不会直接对源进行任何更改。为此,您需要自己调整图像的大小,读取缩放值并将其应用于原始宽度和高度。
看看像AForge.Net这样的东西,它有无数种处理图像的方法,可以控制转换的质量等。