WPF - 旋转和保存图像 - 用于视图和磁盘

发布于 2024-08-30 15:45:31 字数 2043 浏览 7 评论 0原文

我有一个包含图像控件的视图。

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

绑定使用的转换器除了将 BitmapCacheOption 设置为“OnLoad”之外没有任何其他作用,以便在我尝试旋转文件时将其解锁。

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

这是我旋转图像的代码。 val 始终为 90 或 -90,path 是我要旋转的 .tif 文件的完整路径。

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

我遇到的问题是,当我将 BitmapCacheOption 获取到 BitmapCacheOption.OnLoad 时,文件未锁定,但旋转并不总是旋转图像(我相信它正在使用每次的原始缓存值)。

如果我使用 BitmapCacheOption.OnLoad 因此文件未锁定,那么图像旋转后如何更新图像控件?原始值似乎缓存在内存中。

是否有更好的替代方法来旋转视图中当前显示的图像?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-09-06 15:45:31

又一年后,我需要这样做并找到了以下解决方案

如果您有图像元素:

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

您可以在代码隐藏中旋转它:

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

或者,假设您的图像元素名为 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 ;
}

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 ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文