Set image offset of ImageBrush? - c#

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

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
The Image
The Width of he cropped portion
The Height of the cropped portion
The Left of the cropped portion
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.

Related

Win2D draw single image

I'm able to fill a rectangle with an image and i apply a mask on top of the image using this code
args.DrawingSession.FillRectangle(rect, imgRnd, mask);
i need to apply some transform to this image, i'am able to do that with no issue, but i have encounter a strange issue, the last pixel is repeated.
i have used
imgRnd.ExtendX = CanvasEdgeBehavior.Wrap;
imgRnd.ExtendY = CanvasEdgeBehavior.Wrap;
and the image is repeated continuously.
My question is : there is a way to draw one time the image disabling and ExtendX and ExtendY?
FillRectangle will always fill all the pixels within the specified rectangle. The edge behavior enum controls what value they are filled with if the image is positioned such that it does not completely cover the rectangle being drawn.
How exactly are you transforming the image? Can you change that to also transform the rectangle itself, so you won't be trying to fill pixels that aren't covered by the image?
Another option is to use image effects (Microsoft.Graphics.Canvas.Effects namespace) which give much more detailed control than FillRectangle over how multiple images are transformed, combined, etc.

Crop stroked region as Bitmap from InkCanvas Container

I am just started working in WPF application. Here, I need to get the signature from user. I am using Inkcanvas control to implement this feature.
I am curious on knowing is there a way in Inkcanvas control to crop the stroked region alone with some outer margin. I don't want the empty space of the Inkcanvas container in the output bitmap image. if the signature in too small, I want to crop the region and stretch that into particular size(300x200).
OK, first you need to determine the rectangle to crop (the "stroked region"), then save it to an image.
InkCanvas has a Strokes property that is the collection of ink strokes, you can get the bounds of each Stroke by calling Stroke.GetBound method. Then you can get the Left property of the "stroked region", which is the Left property of the leftmost bounds. And you also get the Right, Top and Bottom in the same way. I hope you can understand my explanation.
Resizing the cropped image to 300x200 should be quite easy, you can find plenty of answers on Stackoverflow.
I think this changed for Windows 10 Universal. In some ways, while different, it may be a bit easier.
With your InkCanvas, you have InkPresenter.StrokeContainer and from there you can get the BoundingRect property, which will give you a ton of details about the bounding rectangle of your strokes (ie x, y, width, height, left, right, etc...)
So, here is what I did:
var bounds = myCanvas.InkPresenter.StrokeContainer.BoundingRect;
var left = bounds.Left;
var right = bounds.Right;
//and so on...
Once you have the bounding rectangle data, it is very easy to crop.
Hope this helps!

How to display scaled image without anti-aliasing?

Question
How can I scale an image in XAML quickly without anti-aliasing applied?
Background
I am trying to make a pixel editor as a Windows 8 XAML/C# app. I'm using c#/XAML because most of my experience is with c#/WPF.
Method 1: WriteableBitmap + Image control. Originally, I used a WriteableBitmap to store and edit an image. That image is displayed in a resized XAML Image control. The problem is that the image does not get scaled properly because of anti-aliasing. (XAML does not seem to provide the BitmapScalingOptions that are available in WPF)
Method 2: Redrawn WriteableBitmap + Image control. Next I tried writing my own scaling, where I take my original image and write to a larger WriteableBitmap so that the scaled image is pixelated. The scaled image is then presented inside a XAML Image control. This process is slow and inefficient.
Method 3: SharpDX + Direct2d ?? I am pretty sure a solution exists somewhere between SharpDx, SurfaceImageSource, and Direct2d. Event still, I can't quite figure out how to display a SharpDx.WIC.Bitmap inside a Windows.UI.Xaml Image control, and am generally getting lost in the documentation.
What exactly is a recommended setup for achieving my desired end? Are there c# samples available which might point me in the right direction?
I don't know if you mean by scaling it by resizing it with the mouse or just with a button click to auto scale to a predetermined size.
But the thing you can do is to use an Image panel and then, just set it to image.Stretch = Stretch.Fill; so if you override and create a method for the Resize event of the Image Panel, your image will take the size to fill the Image panel.

WPF spritesheet animation

I am trying to create an animated sprite from a spritesheet in wpf. The current technique I am using is breaking down an image of containing all the frames of the animation into separate images and just swapping them out.
However this is costly on memory and was wondering if there is a way to just draw a section of an image (specify the source rectangle) rather than splitting the image up.
You can definitely do this. In order for this to be supported by WPF's built-in animation system you need to animate a dependency property. In your case it seems you want to animate the SourceRect property of a CroppedBitmap using a RectAnimationUsingKeyFrames with the DiscreteRectKeyFrame class.
If you're displaying the image using an Image element then you would set it's Source property to a CroppedBitmap, then the cropped bitmap would have its Source property set to a BitmapImage. The cropped bitmap wraps the standard bitmap and adds cropping functionality.

How do I get the dimensions of the ImageRectangle in PictureBox?

Background
I want to be able to get the drawn dimensions of a zoomed image inside the picturebox (I'll explain below).
The PictureBox.ImageRectangle property seems to be exactly what I'm looking for because it shows the height and width of the resized image, as well as it's relative top, left position inside the control.
The trouble is PictureBox.ImageRectangle is private and so I can't read the values without using reflection (which is obviously not ideal).
Actual Question
My question is, is there another way that I can easily get to these values without writing a method to calculate what the values "ought" to be? I can do that easily enough, but I feel I'd be reinventing the wheel.
Context:
I'm writing a simple image processing app in C# and one of the things it has to do is allow the user to draw a selection around a portion of the image (a lot like the Marquee tool in Photoshop).
I need to know the dimensions of the rendered image so I know where to set the bounds of my marquee tool and also to translate the values of the drawn rectangle to points on the scaled bitmap inside the control.
My answer look simple so maybe I'm missing something, but I think Control.DisplayRectangle suits your need.
EDIT
OK, missed the point; however see How to get the value of non- public members of picturebox?
if you want to access dimension of the image in picture box you can use
GraphicsUnit units = GraphicsUnit.Point;
RectangleF imgRectangleF = pictureBox1.Image.GetBounds(ref units);
Rectangle imgRectangle = Rectangle.Round(imgRectangleF);

Categories