开发者

WPF - Rotate and Save Image -both for the View and to Disk

开发者 https://www.devze.com 2022-12-28 03:43 出处:网络
I have a View that contains an image control. <Image x:Name=\"img\" Height=\"100\" Margin=\"5\" Source=\"{Binding Path=ImageFullPath Converter={StaticResource ImagePathConverter}}\"/>

I have a View that contains an image control.

<Image
x:Name="img"
Height="100"
Margin="5"
Source="{Binding Path=ImageFullPath Converter={StaticResource ImagePathConverter}}"/>

The binding uses a converter that does nothing interesting except set BitmapCacheOption to "OnLoad", so that the file is unlocked when I attempt to rotate it.

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    // value contains the full path to the image
    string val = (string)value;

    if (val == null)
        return null;

    // load the image, specify CacheOption so the file is not locked
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.UriSource = new Uri(val);
    image.EndInit();

    return image;
}

Here is my code to rotate the image. val is always 90 or -90, and path is the full path to the .tif file I want to rotate.

internal static void Rotate(int val, string path)
{
    //cannot use reference to what is being displayed, since that is a thumbnail image.
    //must create a new image from existing file. 
    Image image = new Image();
    BitmapImage logo = new BitmapImage();
    logo.BeginInit();
    logo.CacheOption = BitmapCacheOption.OnLoad;
    logo.UriSource = new Uri(path);
    logo.EndInit();
    image.Source = logo;
    BitmapSource img = (BitmapSource)(image.Source);

    //rotate tif and save
    CachedBitmap cache = new CachedBitmap(img, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
    TransformedBitmap tb = new TransformedBitmap(cache, new RotateTransform(val));
    TiffBitmapEncoder encoder = new TiffBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(tb)); //cache
    using (FileStream file = File.OpenWrite(path))
    {
        encoder.Save(file);
    }
}

The issue I am experiencing is when I get the BitmapCacheOption to BitmapCacheOption.OnLoad, the file is not locked however rotate does not always rotate the image (I believe it is using the original cached value each time).

If I use BitmapCacheOption.OnLoad so the file is not locked, how can I update the Image control once the image开发者_开发知识库 has been rotated? The original value seems to be cached in memory.

Is there a better alternative for rotating an image that is currently being displayed in the view?


Yet another year later, I needed to do this and found the following solution:

If you have an image element:

<grid>
    <stackpanel>
        <Image Name="img" Source="01.jpg"/>
        <Button Click="Button_Click" Content="CLICK"/>
    </stackpanel>
</grid>

you can rotate it in code-behind:

private double rotateAngle = 0.0;
private void RotateButton_Click(object sender, RoutedEventArgs e)
{
    img.RenderTransformOrigin = new Point(0.5, 0.5);
    img.RenderTransform = new RotateTransform(rotateAngle+=90);
}

Or, assuming your image element is called img:

private double RotateAngle = 0.0;
private void Button_Click(object sender, RoutedEventArgs e)
{
    TransformedBitmap TempImage = new TransformedBitmap();

    TempImage.BeginInit();
    TempImage.Source = MyImageSource; // MyImageSource of type BitmapImage

    RotateTransform transform = new RotateTransform(rotateAngle+=90);
    TempImage.Transform = transform;
    TempImage.EndInit();

    img.Source = TempImage ;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号