Drawing a Grid of Dots on a PictureBox in C# - c#

I have been searching for about 12 hours now trying to find a way to draw dots on a PictureBox, I've found many threads giving example code and yet I just can't seem to get done what I want.
In essance what I am trying to do is this:
I have a windows form with a PictureBox on it, I do not have any Image in the PictureBox, however I do have the BackColor set to Black. I am trying to create a new bitmap image then run code to create white dots in the following style:
..........
..........
..........
..........
Thus giving me a grid style Look on the PictureBox. However at every attempt I have failed, so if anyone could help me understand how to work with this I would appreciate it.
My most recent attempt was to use the ControlPaint.DrawGrid Method, like so:
private void picBox_Display_Paint(object sender, PaintEventArgs e)
{
Size size = new Size(35, 35);
Rectangle rect = new Rectangle(0,0,picBox_Display.Width, picBox_Display.Height);
ControlPaint.DrawGrid(Graphics.FromHwnd(picBox_Display.Handle), rect, size, Color.White);
}
The above code is in the PictureBox Paint event method. I know it runs through the code because I have a breakpoint at the end of the method, but nothing happens. I'm not sure I understand how the ControlPaint.DrawGrid works am I supposed to be adding something else?
I tried using the Bitmap.SetPixel method earlier today but kept having issues with it and kept looking for other ways to try to get it done.
Any help would be appreciated. Thanks!

You need to use e.Graphics for this. Note also that debugging this sort of code can be difficult because debugging often invalidates the drawing so it needs to be drawn again. The last parameter is meant to be the background color against what you are painting, so it looks like it draws the opposite of what you specify. If you background is black you need to pass in Color.Black
ControlPaint.DrawGrid(e.Graphics, rect, size, Color.Black);

Related

Transparent images not showing properly [duplicate]

I've got this topimage with alpha channel in it and I need to put this image over another background image, while the alpha channel from the top image stays intact obviously.
Now I've seen some tutorials with Canvas, but my project doesn't seem to recognize Canvas.
Anyone got an idea why I cant use Canvas or how to put those 2 images over each other?
Ok, I will try to answer: after loading the image, like this more or less, pseudocode:
Bitmap bmp = new Bitmap("MyCooolSemiTransparentImage.png");
bmp.MakeTransparent(colorHaveToBeRenderedTransparent);
colorHaveToBeRenderedTransparent is a color wich results non transparent after loading it into Bitmap object.
EDIT
if alphachannel is ok, here is a simple tutorial how to draw in image on WinForms:
msdn: DrawImage
Call method provided in yuor forms OnPaint override and you will get what you want.
Hope this helps.
Regards.

graphics.drawline makes line to lare

I am a novice in C# (following night school) and am experimenting with some stuff (haven't seen it yet in class).
I am trying to build a palletizing tool, which displays the optimal way to pallatize boxes on a pallet.
Before coding the alorithm to do so I was looking into how I could display the pallet.
My eye caught the Graphics.Drawline method in combination with an override of the OnPaint methode and I have no trouble drawing the line, but it is always to large!
For testing I set my form to 500*500 and I want a line of 500pxl. I would asume if I resize the window that I would immediatly see the end of the line, but it still goes on for about 5-7pxl.
Is there a difference in metrics or something? Below an example of the code I use.
Thanks for any help, couldn't find any related topics on the web that would explain this (perhaps it's just that obvious...).
protected override void OnPaint(PaintEventArgs paintEvnt)
{
// Get the graphics object
Graphics gfx = paintEvnt.Graphics;
// Create a new pen that we shall use for drawing the line
Pen myPen = new Pen(Color.Black);
// draw pallet
//lenght1
gfx.DrawLine(myPen, 0, 50, 500, 50);
//width1
gfx.DrawLine(myPen, 500, 50, 500, 100);
}
This is most likely being caused by the window you're drawing this in. If you look around the edges of a windows form popup (assuming you're using a windows forms application) you'll see a border.
This border is included in the width of the window. That's why the line seems to go on for a little bit past the edge of your window - you can't see the actual end of the line until you stretch the window and move its border.

Drawing an image onto a Panel control gives artefacts when resizing

Currently I'm trying to do what I thought would be a simple task:
Draw an image onto the full area of a Panel control in Windows Forms. (Please ignore for the moment that I could use the BackgroundImage property)
The image to draw looks like this:
I.e. a yellow box with an 1 pixel blue frame around.
To draw, I'm using the Paint event of the Panel control:
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(Resources.MyImage, panel1.ClientRectangle);
}
This looks fine when initially displaying the form:
When resizing the form (and the docked panel, too), it either cuts the edges when being made smaller...
...or it draws artefacts, when being made larger:
I'm pretty sure that there is going on something rather simple and straight-forward but I really cannot understand the reason.
Since I'm ignoring the ClipRectangle and always draw everything, I thought the image would be scaled all the time.
My questions are:
What is the reason for the artefacts? (I love to understand this!)
What do I have to do in order to get rid of the artefacts? (beside calling Invalidate on each resize)
Update, SOLUTION:
Thanks to Ryan's answer, I was able to find an acceptable solution. Basically I derived a class from Panel, did an override of OnPaintBackground and did not call the base method. Last, I added the following code to the constructor of my derived panel:
base.DoubleBuffered = true;
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();
The reason for the artefacts is that the entire surface isn't redrawn when the form is resized; only the necessary parts are. The generally best solution is what you don't want to do, calling Invalidate on each resize. However, if this is in fact your situation, just use a PictureBox instead. If it's not, you might consider overriding OnPaint in your form instead, and using this.SetStyle(ControlStyles.ResizeRedraw, true) to do this automatically.

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

Gradient Drawing Bug

I'm having trouble with a gradient drawing call. I have a Form that looks like this.
Screenshot http://img413.imageshack.us/img413/3570/30364682.png
The problem is every now and again the above gradient drawing bug will start happening. It should go right across of course. Sometimes it only takes some build-rebuild-mashing to fix and it'll simply just "start" after a build every now and again.
That control (the top white part) is a TableLayoutPanel. The BackColor is set to white and on the panel's Paint event I do this:
/// <summary>
/// Draws the background gradient.
/// </summary>
private void titleBarLayoutPanel_Paint(object sender, PaintEventArgs e)
{
Brush brush = new LinearGradientBrush(titleBarLayoutPanel.Bounds, TaskHeaderLeftColor, TaskHeaderRightColor, LinearGradientMode.Horizontal);
e.Graphics.FillRectangle(brush, titleBarLayoutPanel.Bounds);
}
Should I be doing something else? The problem is that it works, and then without so much as a rebuild or build this will start happening!
EDIT I have since rebuilt the class library it is contained in (it's a generic Form) then rebuilt the app it's used in and the gradient is now filling across completely. This is bizarre.
Building and re-building your application, with no changes, normally doesn't solve this (or most any other bug for that matter) save the ones in which you run your application without doing a clean/rebuild first and then notice that the code you just wrote doesn't run (not sure that's possible these days with the IDEs). I see this a lot with newer devs when they keep rebuilding hoping that somehow the compiler will make the code "correct" or that maybe the compiler is simply not generating the correct code to begin with. (Please note that I do not mean the aforementioned statements to be taken disparagingly.)
To solve the issue at hand, you might try deriving your own TableLayoutPanel class in which you override the OnBackgroundPaint event, painting your own background, or simply returning if you don't want to paint your own background. (You seem to be painting the background in the Paint event). What you are doing in the code above is simply painting over the background already painted by the control, hence the "bug" you see, (double paint). It appears that the form is not resizable. Try making it resizable. Then resize it and watch it paint, or simply move other windows over it.
class CustomTableLayoutPanel : TableLayoutPanel
{
protected override void OnPaintBackground(PaintEventArgs e)
{
Brush brush = new LinearGradientBrush(this.ClientRectangle, TaskHeaderLeftColor, TaskHeaderRightColor, LinearGradientMode.Horizontal);
e.Graphics.FillRectangle(brush, this.ClientRectangle);
//base.OnPaintBackground(e);
}
}
By the way, you should replace Bounds with ClientRectangle.
Bounds is the control's rectangle relative to its parent; ClientRectangle is relative to the control itself.
In this particular case, it won't make a difference, since the control is at 0, 0.

Categories