Undo Changes with collection of images - c#

I'm trying to create a undo functionality to a picture box by saving the image right before the paint take place into a array or something. I tried with List but i get the same image all the time, with Image List the size is to small for my porpoises. What are the best practices for saving undo data or how can i store images into memory right before every paint take place? Thank you !

You have to clone the image contents every time. I think you are adding always the reference of the image to the list.
If you want to get undo functionality done properly have a look at the Command Pattern.
Another way would be to calculate the difference of the original image and the image after the operation and then store the difference to save memory.

Many paints will take place on the same image. Windows fires the Paint event whenever part of the control needs to be repainted. Like when you drag another window across the PB. Or minimize and restore the form.
In other words, Paint doesn't tell you that the image changed. You'll need to derive your own class from PictureBox and override the Image property. The setter will be called when the image is changed.

Related

How can I draw directly to the PictureBox.Image property (if possible)?

I have been using the Windows Touch Scratchpad Sample in C# (MTScratchpadWMTouchCS) example located at https://msdn.microsoft.com/en-us/library/windows/desktop/dd940546(v=vs.85).aspx and it works beautifully.
What I would like to do, is set the Form.BackgroundImage to a BMP file, which works fine at the moment.
Next, I would like to be able to 'scribble' over the form using a touch device - also working without issues at the moment.
Then, I to use a Button to use 'DrawImage' to place another BMP file (or Bitmap object) of an 'Emoji' (for example) onto the form, and be able to retreive it using the Form.Image property. Also working fine.
Lastly, and this is where my issue is - I need to be able to 'grab' the Form.Image property and be able to save it to a file. This almost works, in that it grabs the 'emoji' but it does not grab the 'scribble'
So looking at the Example linked above, it appears that it does not 'draw' to the Image property of the form. Is there a way I can acheive this? Graphics have always eluded me.
First of all, that's really a lot of questions so you should deal with them one at a time. But to address the general aspects of your problem:
Don't work directly on the form. Add controls to the form that you can interact with. For instance, I would add a Panel and make it take up the entire client area (Fill mode). Then, I would add controls to that Panel, such as a PictureBox. You can either subscribe to the controls' Paint events, or even inherit your own control and override the Paint event for full control. Depends on how much you need to get a hold on.

Get current display Picture Box image while its image and background image are null

The picture box Handle is passed to the external library.
After specified event is triggered, then result picture is display on the picture box.
I tried to get the picture box content to save to picture but I found that
pictureBox.Image = null
pictureBox.BackgroundImage = null
How can I get the image displaying at picture box beside of these two?
Your external library is using the handle to draw directly into the PictureBox control's window. Unfortunately, this does not in fact result in any sort of permanent assignment of an image. It's strictly a one-time event; not only will it not set the properties you are interested in, it's unlikely that the drawn image will even remain on the screen if the window needs to be updated for any reason (e.g. is overlapped by another window and then exposed again).
Without a lot more details about the external library, it is not possible to suggest a specific solution. Ideally, the library itself will offer some alternative mechanism for it to deliver the image you want. Then you can use that data to initialize a managed Bitmap object which can then be assigned to your PictureBox as its Image. I guess one hack would be to let the library draw into the window, and then use Graphics.CopyFromScreen() to copy straight from the screen's own graphics buffer the data you want.

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.

WPF control for image manipulation

I need to load an image in a WPF window, an be able to read and modify individual pixels (in an efficient way), zoom the image (and scroll it), get the value RGB/grayscale of the pixel under the cursor, select areas (I guess knowing the cursor position and being able to modify pixels I could draw myself the square which represents the selected area)...
What is the best combination of WPF controls and classes to accomplish this?
I've been trying to do it loading a System.Windows.Media.Imaging.BitmapImage and putting it into a System.Windows.Controls.Image, but it's taking much longer than I expected.
Thank you very much
I once used this WPF Interactive Image Cropping Control. Go check it out, it should at the very least give you a good place to start. Oh, and welcome to Stackoverflow. :)

GDI+: Alternatives to DrawReversible for visual selection indicators?

I've searched around for an alternative way of drawing selection indicators for visual objects (like selected edges, lines etc.) without the use of ControlPaint.DrawReversibleFrame and related XOR methods. The reasons are unwanted XOR-ing "artifacts", reversibility not applying to bitmaps, no control of the actual visual look and slowness.
On the other hand I want to avoid having to repaint the whole scene (map actually) if a user decides he wants to deselect an object or two, because the repaint could be quite expensive.
So the only alternative I can see is implementing some basic drawing logic directly on a Bitmap, but with storing the previous contents of the pixels before they change. Then (in theory) I would be able to reapply old contents of, say, an selected edge rectangle if the user chooses to deselect that edge.
My question is whether you think this is a good idea or do you see some other alternatives to my problem (within the GDI+)?
Thanks in advance
If the selection indicator is just drawn on the top of the unselected object, you can use two bitmaps, draw all the unselected objects on the background one and the selection indicators on the other, and paint them both on screen.
Else, you can do the same, except that you render the selected objects instead of just indicators.
Only store the rectangles "of interest" in an off screen buffer. And repaint when the focus is lost. . . Or if you can redraw just the portion as it appears normally based on in memory data you should be fine. Otherwise it seems that you have the gist of it.

Categories

Resources