How to simulate negative padding in picturebox? - c#

What possibilities are there to simulate negative padding in a picturebox in C#? I want to be able to show a cropped version of an image without actually cropping it. When "cropping" the right or bottom side it's easy just to change the size of the picturebox to hide a part of the image, but how do I get the picturebox to hide some pixels on the left or top sides without either cropping the original image or creating a new cropped version of it?

Related

How can I capture as bitmap only what a picturebox is displaying, without using "copy from screen"?

Specifically: I need to capture as a bitmap a specific region of what a picturebox is actually displaying. The coordinates of the region are specified by the bounds of a control that I have overlayed on top of the picturebox (but that belongs to the picturebox). The control is hidden when I make the "snapshot" of the region.
I tried using normal screen capture methods (CopyFromScreen), but you can't really control the timing there. So it was capturing "interstitial" states, like transitions between photos in my picturebox. Frequently it was only capturing purely black images (the background color of the picture box).
So I tried just converting the image (picturebox.image property) being displayed to a bitmap. The problem there is that the picture box is rarely showing exactly the image. It's displaying some PORTION of the image, scaled and clipped as appropriate to it's sizemode (which is zoom). So the I can't just take my control coordinates and clip them from the image as a whole.
So I tried to estimate what portion of the image was being displayed, and correcting my rectangle based on that. Turns out that I was basically re-creating the "zoom" code of the picturebox to do this (using aspect ratio of the picturebox, the aspect ratio of the image, guessing at what level of scaling is currently happening to the image if it's larger or smaller than the picturebox, etc). It was not pretty.
So: now I need a method of just capturing only the bitmap currently being displayed in the client area of the picturebox, including the photo and any black "letterboxing" currently being displayed around it. Anybody got one?
Remember that I can't rely on using CopyFromScreen. It's not reliable enough for my purposes. I think I need a method of getting picturebox to TELL me the bits it is displaying.
This will copy and save the currently shown content of the PictureBox including a BackgroundImage (if there is one and if it shines through) and also all Controls that belong to the PictureBox, like Labels etc.. Also included are elements drawn in the Paint event. Things drawn outside the Paint event are non-persistent and will not be included.
using (Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
bmp.Save(yourfilename, ImageFormat.Png);
}
Note: On my test Form the PicureBox is sitting inside an AutoScroll Panel pan_PBscroll. The PictureBox is displaying pixels 1:1 and is therefore, with a photograph loaded, much bigger than the Panel, the Form or even the Screen. So to clip to the actually visible parts I could not use the pictureBox1.ClientSize and pictureBox1.ClientRectangle but used the dimensions of that Panel. This may well apply to you, too.
I'm not sure about your timing issues. But since you mentioned CopyFromScreen here are a few differences:
CopyFromScreen makes a 1:1 copy of each screen pixel
This includes non-persistent drawings and excludes anything covered or hidden
Control.DrawToBitmap makes the Control draw itself onto a Bitmap, just as it draws itself during Paint
This excludes anything that doesn't belong to the Control but includes all members of its Controls collection
This also excludes non-persistent drawings but includes the full Size of the Control, whether it fits on the Form or Screen or not and whether it is hidden or covered or not.
For Controls with active Scrollbars only the visible parts are copied. To copy all you need to resize it temporarily. Then you can get a complete image of a listbox even if it has a thousand items..
Since you're using a PictureBox I would say to take a look PictureBox.Image where you can get the Bitmap object.
Hope it helps.

How to determine rendered size of graphics in Image Control

I have an Image control set for Uniform stretch. It is wider than tall (landscape). I am putting a graphic (source) in it that is taller than wide (portrait). This means that the top and bottom of the graphic match the top and bottom of the Image control and there is 'dead space' on either side of the graphic. Got the picture? (pun intended)
At this point I zoom in and out of the graphic using a Matrix.ScaleAtPrepend on the Image control and pan using Matrix.OffsetX and OffsetY. Works great.
Now I would like to click on the panned/zoomed graphic and get the pixel coordinates of my click relative to the graphic and not relative to the Image control.
How might I do that?

Create an effect of magnifying glass for a picturebox

I would like to know how to create an effect of a magnifying glass for a picturebox.
Not zooming the picturebox but magnifying a part of the the image in the PictureBox control (circle or rectangle) and setting the size of the glass and the magnification factor.
It may only work within the picturebox control.
Language: C#
Thanks in advance !
Basically, you'd need two pictureboxes. One for the whole image and another for the magnified section. Also, you have to place the magnified picturebox according to user's mouse position.
You'll find a good article about it at http://www.codeproject.com/Articles/21097/PictureBox-Zoom. Just change the source to show the second picturebox in appropriate place (under user's cursor position).
You need 2 picturebox objects, one for picture itself and second for magnified area.
Next load picture into memory, you haven't specified source of the picture but in any case I recommend using streams.
Then create bitmap image in memory.
Using Image method set property of a picturebox.
To create source image for magnifying picturebox you need to clone selected part (calculating dimensions of a new picture area). Whole thing is not as trivial as you may expect as clone method accepts Rectangle objects as an area selector and generally works on rectangles rather than circles to copy selection. I also recommend to Dispose() unused bitmap objects as soon as possible.
Hope this helps.

Copy of the screen area

I need to move the black square in an arbitrary area of ​​the screen and copy screen area inside black square to area in my application.
How to do it? Thanks.
Take a look at Graphics.CopyFromScreen():
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.copyfromscreen.aspx
As for the movable black rectangle, I'd suggest making a transparent form and manually painting the border as desired. Use the client area size and position of the form as a hint as what to pass to CopyFromScreen().

C# Image over another image

I have a picturebox with a fixed sized image (256x256) generated by the program. I have another smaller image as a resource. What I want to do is when my cursor is over the image and I hold down the mouse button, the smaller image "anchors" with the mouse pointer so it moves around with it. If I let go of the mouse button, the smaller image will stay in that position on top of the bigger image. The smaller image is basically a marker, something like an X or O.
I was thinking of having a second picturebox on top of the first picturebox but I can't make it transparent. Or redrawing the image with the smaller image on top of it and reloading the image into the picturebox, but I'm not sure how to do that and I think it's going to be pretty slow redrawing it each time I move the mouse.
So how can I have a marker image move around on top of a bigger image and have it stay there?
Create your control for this instead of using PictureBox. PictureBox should be used ONLY for fixed images on the form, nothing else.
Instead, derive your control from UserControl. Turn on double buffering for it. In OnPaint method, first draw your background picture, then your marker picture after it. Don't worry, it WON'T be slow and it WILL work as it should.
When you release the mouse, update background picture by drawing your marker picture on it.
Since every sentence here is a little discovery by itself, hope you'll have a good time coding your little game :)

Categories

Resources