Finding the background colour System.Drawing.Bitmap - c#

After I've drawn an object in the bitmap, I'd like to move it, so I need to draw the object again in the background colour, before drawing again in the proper colour. Assuming this is the correct approach, how do I find out what the background colour actually is?
Thanks,
James

Once you draw your object you can only use GetPixel to obtain the colour:
Bitmap.GetPixel(x,y)
Why not just clearing the whole bitmap and rewriting your object again?
If your bitmap is not extremely large, you won't probably even notice any difference in performance.

Since you have control on the canvas and if you have a "clean" canvas, you can get the color of any pixel before drawing and that should be your background color.
But remember, you don't have a background color on a regular bitmap, just a bunch of pixels! So if your canvas is not clear, or if you don't have a way to get the color before drawing, you have to make assumptions. There a quite a few solutions depending on your requirements. My suggestions are:
You can assume the pixel on the corner are your background colors
The color that appears more often is the background color

Related

C# Removing a certain line in Graphics/Drawing

Is it possible to remove some lines in my graphic panel without erasing the rest?
PanelGraphics.Clear(Color.Black); erases all the graphics but I only want to remove one line, or a certain color.
Is this possible?
Is it possible to remove some lines in my graphic panel without erasing the rest?
Not really. The Graphics does not tell you anything about the actual context, it can be the screen, a printer, a bitmap, a metafile, the surface of a control or something similar. If you draw a Panel or a control you must always re-draw the whole content once it is invalidated. Otherwise, you will get a messy result. Just try to move your window partly out of the screen, the custom-drawn content will be "erased" unless you redraw it.
However, if you have a concrete image, such as a Bitmap, you can replace its colors; however, you must know the exact PixelFormat of the image. See my answer here if you are interested: https://stackoverflow.com/a/33562891/5114784

Find the center of a set of graphics

I have a form with a panel where a set of complex graphics are painted (represented by blue circles in the image):
The graphics are painted using DrawLine and DrawEllipse methods of the Graphics class.
I need to find the global center of the set of graphics (yellow point on the image) so that I can apply some transformations afterwards, like centering the set of graphics on the panel.
The most direct workaround I can think of is finding the boundaries of the graphics, so they are contained in a rectangular frame (red box on the image) and then divide the frame's width and height by two.
But how can I achieve this? Is there a method that would allow me to find the top, bottom, left and right boundaries of the entire set of graphics?
I'm not aware of any method in Graphics to do what you're looking for, but rolling your own should be relatively easy assuming you know the dimensions of each component image. The various DrawEllipse methods each take a bounding rectangle in some form, so you've already got all of the information that you need.
The most straightforward approach would be to simply keep track of the farthest left, right, top, and bottom bound of each image as you draw it and then form the complete bounding rectangle from those dimensions. You'll also need to account for the width of the Pen that you're using to draw your images.
As an alternative, you might want to take a look at the GraphicsPath class. GraphicsPath.GetBounds would more or less give you what you're looking for. If GraphicsPath doesn't work for you, then I think you're probably stuck writing your own code.

Making scribbles on a monochrome image

Imagine there's a picturebox which loads a monochrome image. And there is a need to make few color scribbles on it. I have no background with graphics. Would it be just a pen drawing pixels or something more complex I don't know.
Target language is C#. Technology: WinForms.
I think the easiest way to achieve what you want would be to create a very lightweight retained mode drawing system. Keep track of all the positions where the user has scribbeled and draw dots/circles/lines/rubberducks/whatever at these positions in the PictureBox's Paint event. On mousedown+move events, call the PictureBox' Invalidate() function. The original picture must either be painted underneath or in the class' OnPaintBackground (which IMO is more elegant).
This tutorial should get you started:
https://web.archive.org/web/20121006140255/http://www.bobpowell.net/backtrack.htm

Drawing, and Appending to a Double-Horizontal Line

I know how to draw straight lines in C#, but I would like to draw a horizontal line, with two colors. Light blue on the top, and dark blue on the bottom. Also, how would I sort of... Append to that line? For example, every few seconds, that line will become bigger... like a ProgressBar. (btw, I'm not creating a ProgressBar, just using that as an example).
Here's the kind of line that I'd like to draw. I am aware that I can simply use PictureBox. But I want to start drawing!
Also, if there are other ways of drawing, than GDI or GDI+, could you list or link to those?
Thanks!
You can implement the onPaint event on any Control and then do the drawing manually on the passed graphics object.

c# winform drawn image flashing

I have a image drawn in an winform app and i designed a brush that moves after the cursor. The Brush is drawn every time so the image keeps flashing because the image is also redrawn . How can i avoid this ?
Regards,
Alex Badescu
Use double-buffering. Draw each frame to some kind of memory bitmap representing the back buffer and once it's drawn show it on the first.
For more info read here:
http://msdn.microsoft.com/en-us/library/b367a457.aspx
Simply set the form's DoubleBuffered property to true. That should solve the flickering.
No reason to make it more advanced than this, in such a simple scenario.

Categories

Resources