wpf - 为什么WPF使处理图像变得如此困难?

标签 wpf bitmapimage

我曾经能够仅使用BitmapGraphics对象来做到这一点。现在,我一直在使用WPF,我似乎唯一能做的就是加载图像并显示它,并使它在愚蠢的屏幕上跳舞。他们为什么要摆脱这些非常有用的工具。他们是否在试图愚弄.Net?

我要做的就是从文件加载图像并将其切成两部分。使用.Net 2.0和System.Drawing很容易。但是使用WPF时,我遇到的麻烦是没有使用一些非常低级的代码。我尝试过使用WriteableBitmap。但这似乎并不是我想要的。没有办法将DrawingContext包裹在BitmapImage周围吗?

请告诉我,对于应用程序,WPF不仅仅是HTML。我真的很沮丧!

编辑:

此外,到底如何将图像保存到文件?

最佳答案

如果要将图像分为两部分,为什么不使用CroppedBitmap类?

考虑以下XAML。一个源BitmapImage由两个CroppedBitmaps共享,每个显示源的不同部分。

 <Window.Resources>
    <BitmapImage x:Key="bmp" UriSource="SomeBitmap.jpg" />
</Window.Resources>

<StackPanel>
    <Image>
        <Image.Source>
            <CroppedBitmap Source="{StaticResource ResourceKey=bmp}">
                <CroppedBitmap.SourceRect>
                    <Int32Rect X="0" Y="0" Width="100" Height="100" />
                </CroppedBitmap.SourceRect>
            </CroppedBitmap>
        </Image.Source>
    </Image>
    <Image>
        <Image.Source>
            <CroppedBitmap Source="{StaticResource ResourceKey=bmp}">
                <CroppedBitmap.SourceRect>
                    <Int32Rect X="100" Y="150" Width="50" Height="50" />
                </CroppedBitmap.SourceRect>
            </CroppedBitmap>
        </Image.Source>
    </Image>
</StackPanel>


更新:在代码中执行类似的操作:

        var bitmapImage = new BitmapImage(new Uri(...));
        var sourceRect = new Int32Rect(10, 10, 50, 50);
        var croppedBitmap = new CroppedBitmap(bitmapImage, sourceRect);

关于wpf - 为什么WPF使处理图像变得如此困难?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471734/

相关文章:

c# - 为什么我不能在将控制台窗口作为我的 WPF 应用程序的父级后与它交互?

android - 空对象引用上的 Bitmap.getWidth()'

c# - 如何将 WriteableBitmap 转换为 BitmapImage?

c# - 在 WPF 中更改 BitMapImage 的尺寸,我可以在 <Image> 元素中放置什么样的对象?

wpf - 如何更改 Slider 的 Track 模板?

wpf - 如何在调整窗口大小时调整 ListView 控件的大小

c# - WPF查找窗口实例

wpf - 你如何处理 MVVM 中的 ComboBox SelectionChanged?

c# - BitmapSource 到 BitmapImage

c# - 如何将 'System.Windows.Media.Imaging.BitmapImage' 转换为 'System.Drawing.Image' ?