mpen
mpen

Reputation: 282885

Set image offset of ImageBrush?

I've got a large image in memory which I convert to an System.Windows.Media.ImageBrush and use it as the Fill for a System.Windows.Shapes.Rectangle. You can move this rectangle around with your cursor.

Basically I want to use the rectangle as a "viewport". Thus I need to change which parts of the image get displayed within the rectangle, i.e., define a rectangular subsection of the image.

How can I do that?

I see ImageBrush.Viewport but that doesn't seem to mean the same thing.


I'm open to alternative solutions that don't involve a rectangle, such as drawing directly on a canvas or something, but AFAIK WPF doesn't let you access pixel data directly (at least not easily).

Upvotes: 2

Views: 1457

Answers (1)

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

To achieve this your going to have to create your own rectangle user control to allow the user to create/resize a rectangle. Then I would create a CroppedBitmap of the image in the rectangle portion
Cropped Bitmap MSDN
Stackoverflow example

Edit

No, no, no @Mark, You dont turn the CroppedBitmap into a UserControl. You create a USerControl that exposed the CroppedBitmap. Basically, you create a UserControl with the following DependencyProperties

  1. The Image
  2. The Width of he cropped portion
  3. The Height of the cropped portion
  4. The Left of the cropped portion
  5. Top of the cropped portion

Then as soon as any of these properties your DP callback will do a RenderTargetBitmap Crop of the new region.

Upvotes: 2

Related Questions