3

I used to be able to do so very much with just the Bitmap and Graphics objects. Now that I've been using WPF the only thing I seem to be able to do is load an image and show it and make it dance around the stupid screen. Why did they get rid of these very useful tools. Are they trying to stupefy .Net?

All I want to do is load an image from a file and cut it into two parts. This was easy with .Net 2.0 and System.Drawing. But with WPF, I'm hitting a brick wall without using some very low level code. I've tried working with WriteableBitmap. But it doesn't seem to really be what I'm wanting. Is there no way to wrap a DrawingContext around a BitmapImage or something?

Please tell me that WPF is more than HTML for applications. I am REALLY frustrated!!

Edits:

Also, how on earth does one save an image to a file?

Jordan
  • 9,642
  • 10
  • 71
  • 141
  • 1
    As for saving images to file (which is a separate question and should be treated as such), try searching for it on the web. Here's one hit: http://blogs.msdn.com/b/kirillosenkov/archive/2009/10/12/saving-images-bmp-png-etc-in-wpf-silverlight.aspx – Peter Lillevold Jun 24 '11 at 18:35
  • Try using OpenGL. You'll find it's faster and more useful than both GDI+ (System.Drawing) and WPF. OpenTK (www.opentk.com) is a great wrapper for .NET around OpenGL. – bbosak Jun 28 '11 at 14:51

3 Answers3

1

If you want to cut the image in two parts, why not use the CroppedBitmap class?

Consider the following XAML. One source BitmapImage shared by two CroppedBitmaps, each showing different parts of the source.

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

Update: to do something similar in code:

        var bitmapImage = new BitmapImage(new Uri(...));
        var sourceRect = new Int32Rect(10, 10, 50, 50);
        var croppedBitmap = new CroppedBitmap(bitmapImage, sourceRect);
Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
  • Interesting, but I'd want to do this below the XAML, as each piece of image is used in two separate controls. I'll bet that this is fairly easy to translate into code though. No? – Jordan Jun 28 '11 at 13:16
  • Absolutely, this is just how you *could* build up the objects in XAML. The same objects (`CroppedBitmap`, `Int32Rect`, etc) can be built in code, no sweat. – Peter Lillevold Jun 28 '11 at 14:24
0

Well there is this http://www.nerdparadise.com/tech/csharp/wpfimageediting/

or perhaps you could add a reference to System.Drawing to your project and then do the editing the way you are comfortable with.

Bueller
  • 2,336
  • 17
  • 11
  • Yeah, I thought about that. Interesting site. I'm trying really hard not be be that low level. If I'm going to do things that low level, I'm going to write my own reusable library assembly, and that's a lot of time. Maybe... – Jordan Jun 24 '11 at 18:08
0

You are probably best off using TransformedBitmap. Load your Bitmap as a BitmapSource, then set the Transform property to however you want the image to transform. You have several different transformation options here. This allows you to rotate, screw, matrix, etc. transformations. If you want to apply more than one, use a TransformGroup and apply several transformations at once.

You can also use BitmapFrame.Create(...) to work with the transformed image more.

Some Pseudo code:

var image = new BitmapSource(...); //Your image
var transformBitmap = new TransformedBitmap(image);
var transformBitmap.Transform = ..//Set your transform;
//optionally:
var frame = BitmapFrame.Create(transformBitmap);
vcsjones
  • 138,677
  • 31
  • 291
  • 286