在我的WPF中,我创建了一个名为“ image_box”的图像框
在Window_Loaded上,我用
加载我的图像框image_box.Source = new BitmapImage(new Uri("pack://application:,,,/images/pic.png"));
我在Rotate_Click(对象发送者,RoutedEventArgs e)上有下一个代码
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri("pack://application:,,,/images/pic.png");
bmp.EndInit();
TransformedBitmap myRotatedBitmapSource = new TransformedBitmap();
myRotatedBitmapSource.BeginInit();
myRotatedBitmapSource.Source = bmp;
myRotatedBitmapSource.Transform = new RotateTransform(90);
myRotatedBitmapSource.EndInit();
image_box.Source = myRotatedBitmapSource;
我想要的是这段代码
bmp.UriSource =新的Uri(“ pack:// application:,,, / images / pic.png”);
使用
image_box的位置
bmp.UriSource = image_box.Source;
我尝试
Uri ur = new Uri(image_box.Source.ToString());
...
bmp.UriSource = ur;
但在第二次点击我得到无效的网址
答案 0 :(得分:0)
表达式
image_box.Source.ToString()
仅在Source
是BitmapImage时返回有效的URI字符串。但是,在您的Click处理程序的第二次调用中,Source是TransformedBitmap
。
您应该简单地重用原始源图像并将旋转角度增加(或减小)90的倍数。
private BitmapImage source = new BitmapImage(new Uri("pack://application:,,,/images/pic.png"));
private double rotation = 0;
private void Rotate_Click(object sender, RoutedEventArgs e)
{
rotation += 90;
image_box.Source = new TransformedBitmap(source, new RotateTransform(rotation));
}
或者也保留TransformedBitmap并仅更改其Transform
属性:
private BitmapImage source = new BitmapImage(new Uri("pack://application:,,,/images/pic.png"));
private double rotation = 0;
private TransformedBitmap transformedBitmap =
new TransformedBitmap(source, new RotateTransform(rotation));
...
image_box.Source = transformedBitmap; // call this once
...
private void Rotate_Click(object sender, RoutedEventArgs e)
{
rotation += 90;
transformedBitmap.Transform = new RotateTransform(rotation);
}