CropImage Method ?

Jan 29, 2010 at 5:25 PM


Great project.

Can you please add new method into your project like CropImage?

realisation please look at here


Jan 29, 2010 at 6:56 PM

Thanks for your suggestion.

I've just checked a similar method in. :) See WriteableBitmapBaseExtensions.cs  Crop()

Jan 29, 2010 at 7:27 PM

cool thanks :)


Jan 29, 2010 at 7:51 PM
Edited Jan 29, 2010 at 8:01 PM

i write engine for rpg game and i need to crop images from Image type.


what about overloaded method like below?


public static WriteableBitmap Crop(this Image bmp, int x, int y, int width, int height)
   WriteableBitmap result = new WriteableBitmap(width, height);
   if (bmp != null)
      WriteableBitmap tempBitmap = new WriteableBitmap(bmp.Width, bmp.Height);
      tempBitmap.Render(bmp, new MatrixTransform());



Jan 29, 2010 at 8:27 PM

This wouldn't be a WriteableBitmap extension method. I don't  understand your snippet, but I guess you want to crop an Image and reassign it to the original image, right?

Why don't you just call:

myImage.Source = new WriteableBitmap(myImage, null).Crop(x, y, w, h);

Jan 29, 2010 at 8:46 PM
Edited Jan 29, 2010 at 8:48 PM

"but I guess you want to crop an Image and reassign it to the original image, right?"

no, i need to crop tile from big tileset.


As you can see I need to do additional steps to convert the image.

well, if you do not want to add an overloaded method,
maybe add a converter (from Image to WriteableBitmap) into WriteableBitmapConvertExtensions ?

Jan 29, 2010 at 9:14 PM

If you want to "convert" an Image to a WriteableBitmap you can use the Render method or just a WriteableBitmap constructor, so a convert would only be a one liner:

// Image to WriteableBitmap "conversion": Render Image to WriteableBitmap
var wb = new WriteableBitmap(myImage, null);

// ...
// Crop it var croppedWb = wb.Crop(x, y, w, h); // Assign to another image control anotherImage.Source = croppedWb;

Jan 29, 2010 at 9:20 PM

I understood how to use a converter.

I do not understand why this converter so difficult to include in your library?

Jan 29, 2010 at 10:03 PM

It's not really a converter nor an extension method. It's actually a rendering of the vector UIElement to WriteableBitmap.

And it's adds not much functionality to the lib. The method would only be:

public static void FromUIElement(this WriteableBitmap bmp, UIElement elem)
   bmp = new WriteableBitmap(elem, null);

// or
public static WriteableBitmap ToWriteableBitmap(this UIElement elem)
return new WriteableBitmap(elem, null);

Instead of:

var wb = new WriteableBitmap(myImage, null);

you could write:

var wb = myImage.ToWriteableBitmap();

or even:

var wb = new WriteableBitmap(w, h);

Is this realy that better? And the ToWriteableBitmap() method would actually be an extension to the UIElement. Sorry, but I have to draw a line somewhere.

Jan 30, 2010 at 6:34 AM

is your lib, do whatever you want.

but I think that the converter would be useful (IMHO).

Jan 30, 2010 at 11:32 AM

You have the code so feel free to add in your custom build, but I would encourage you that you create a feature suggestion so other people can vote for it. If enough voted for I will definitely include it. :)

And thanks again for your great suggestion of the Crop() method.