我曾经能够仅使用Bitmap
和Graphics
对象来做到这一点。现在,我一直在使用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/