簡體   English   中英

wpf使用bitmapimage作為TransformedBitmap的源

[英]wpf use bitmapimage as source for TransformedBitmap

在我的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;

但是第二次點擊我得到了無效的URL

表達方式

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM