【问题标题】:Affecting Streamsource from a bitmap to another not working将流源从位图影响到另一个不起作用
【发布时间】:2016-07-26 17:52:18
【问题描述】:

使用 UriSource 旋转 bitmapImage 可以使用以下代码

    private void RotateDocumentImageSourceRight()
    {
        var biOriginal = (BitmapImage)documentImage.Source;
        if (biOriginal == null)
            return;
        var biRotated = new BitmapImage();
        biRotated.BeginInit();
        biRotated.UriSource = biOriginal.UriSource;
        switch (biOriginal.Rotation)
        {
            case Rotation.Rotate0:
                biRotated.Rotation = Rotation.Rotate270;
                break;
            case Rotation.Rotate270:
                biRotated.Rotation = Rotation.Rotate180;
                break;
            case Rotation.Rotate180:
                biRotated.Rotation = Rotation.Rotate90;
                break;
            case Rotation.Rotate90:
                biRotated.Rotation = Rotation.Rotate0;
                break;
            default:
                break;
        }
        biRotated.EndInit();

        documentImage.Source = biRotated;
    }

但是当我改变 bitmapImage 存储到 StreamSource 的方式时,它不起作用并且图像消失了

    private void RotateDocumentImageSourceRight()
    {
        ...same code...
        biRotated.StreamSource= biOriginal.StreamSource;
        ...same code...
    }

【问题讨论】:

  • 您没有在Image 控件上使用RenderTransform 是否有原因?更便宜、更快。
  • 是的,跟项目有关,就不做改造了。
  • 当您尝试使用StreamSource 时,是否从流中加载了原始图像?否则它将不起作用 - 它将是 null
  • 是的,但旋转后图像变为空并消失:/
  • 这个怎么样:var biRotated = new TransformedBitmap(biOriginal, new RotateTransform(90))。当您指定 Rotation 属性时,BitmapImage 在内部使用类 TransformedBitmap。它将创建一个新图像,但速度会更快(不需要再次读取流)。

标签: c# wpf


【解决方案1】:

您可以使用TransformedBitmap,当您指定Rotation 属性时,BitmapImage 在内部使用它。它将创建一个新图像,但速度会更快(不需要再次读取流)。

private void RotateDocumentImageSourceRight()
{
    var biOriginal = (BitmapSource)documentImage.Source;
    if (biOriginal == null)
        return;
    var angle = 0.0;
    var biRotated = biOriginal as TransformedBitmap;
    if (biRotated != null)
    {
        biOriginal = biRotated.Source;
        angle = ((RotateTransform)biRotated.Transform).Angle;
    }
    angle -= 90;
    if (angle < 0) angle += 360;
    biRotated = new TransformedBitmap(biOriginal, new RotateTransform(angle));
    documentImage.Source = biRotated;
}

【讨论】:

  • 太棒了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多