I'm making a graphics editor for my class project and i want to make so that when, for example a user loads a picture in to the editor or or draw something in the PictureBox, all the alpha parts are shown the chessboard like background.
My idea is that when I create a PictureBox with transparent background set, I create another one behind it, set its BackColor to white and add grey images 50x50, alternately horizontally and vertically. Is that a good approach to the problem? If, not do You have any suggestions?
In Photoshop, for example, I create image 1600x1600. When I zoom to a certain level, it shrinks the boxes and adds more of them to fill the image. If You'we used Photoshop of similar program you know what I mean. Now, how would I go about achieving the same effect?
Creating a Photoshop-like program is a fun project.
There will be many challenges along your way and it is well worth thinking ahead a little..
Here is a short and incomplete list of things to keep in mind:
So getting a checkerboard background is only the start of a long journey..
Using a
PictureBox
as the base canvas is a very good choice, as its several layers will help. Here is a piece of code that will provide you with a flexible checkerboard background, that will keep its size even when you scale the real graphics:Load an Image for testing and this is what you'll get, left normal, right zoomed in:
Yes, for saving this background should be removed; as you can see in the code, passing in a size = 0 will do that.
What next? Let me give you a few hints on how to approach the various tasks from above:
Scrolling:
Picturebox
can't scroll. Instead place it in aPanel
withAutoScroll = true
and make it as large as needed.Zooming: Playing with its
Size
and theSizeMode
will let you zoom in and out theImage
without problems. TheBackgroundImage
will stay unscaled, just as it does in Photoshop. You will have to add some more code however to zoom in on the graphics you draw on top of the PB or on the layers. The key here is scaling theGraphics
object using aGraphics.MultiplyTransform(Matrix)
.Layers: Layers are imo the single most useful feature in PhotoShop (and other quality programs). They can be achieved by nesting transparent drawing canvases.
Panels
can be used, I preferLabels
. If each is sitting inside the one below it and the one at the bottom has the PB as itsParent
, all their contents will be shown combined.Label
directly but create asubclass
to hold additional data and know-how!FlowLayoutPanel
Draw Actions: These are always the key to any drawing in
WinForms
. When using the mouse to draw, each such activity creates an object of aDrawAction
class you need to design, which holds all info needed to do the actual drawing, like:Along with the LayerCanvas class the DrawAction class will be the most important class in the project, so getting its design right is worth some work!
Only the topmost layer will receive the mouse events. So you need to keep track which layer is the active one and add the action to its actions list. Of course the active layer must also be indicated in the Layers Palette.
Since all drawing is stored in List(s), implementing a unlimited undo and redo is simple. To allow for effective drawing and undo, maybe a common action list and an individual list for each layer is the best design..
Image
you only need to draw them not onto their canvas but into the Image. For the difference of drawing onto a control or into a Bitmap see here! Here we have thePictureBox.Image
as the second level of a PB's structure above theBackground.Image
. (The 3rd is theControl
surface, but with the multiple layers on top we don't really need it..)Saving can be done by either by
Image.Save()
after flattening all Layers or after you have switched off the BackgroundImage by telling the PB to draw itself into aBitmap
(control.DrawToBitmap()
) which you can then save.Have fun!