Creating a form with transparent PNG as background in c# - c#

i want to create a transparent form with png as background... which looks verymuch similar to this.
http://cdn.lo4d.com/t/screenshot/800/lili-usb-creator-3.jpg
so far i've used this code
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage(this.BackgroundImage, e.ClipRectangle);
}
but the problem is when moving the part below doesnt update!!
i tried to use
invalidate();
but it keeps on drawing the image over and over making the dropshadow part denser and denser.
is there anything i can do??

this one works great.not very neat but it works upto the mark.
http://www.codeproject.com/Articles/19213/An-Alpha-Channel-Composited-Windows-Form-with-Desi

Related

Create Form fade out transition

How could you - in WinForms - create a black area that (over time) loses/gains transparency and is redrawn constantly so a kind of transition is created? I dont want to create a seperate form, so this would be very helpful and I could rearrange my new items as soon as the black area reaches a transparency of 0.
Left: What i have what at the moment:
Right: What the program looks like on startup
What I would want: A totally black screen
Override the OnPaint method of the form, and fill a rectangle the size of the form with the desired transparency. In this example, transparency keeps increasing:
var transparency = 0;
protected override OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// e.Graphics.DrawImage(Image, imageLeft, imageTop, imageWidth, imageHeight);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(transparency, Black)), this.ClientRectangle);
}
If you want to do this from a Timer event, inside the Tick event Invalidate the form to force the paint.
protected void MyTimer_Tick(object sender, eventArgs e) {
transparency++;
MyForm.Invalidate();
}
If you need to draw on top of the PictureBox, that presents a problem. One way to solve it is to get rid of the PictureBox and draw the image using Graphics.DrawImage before you draw the fade. If you want to show the image but have the black go up against the edges of your animal, then make it's background color of the PictureBox transparent and convert your image to use a PNG or GIF with all around the animal transparent in the original image.
For more complex effects, Check out the dot-net-transitions project on Google Code. It supports a variety of linear/non-linear transitions.

c# Override high-contrast for background image

I wasn't able to find an answer for this:
I have a background image on my form that I want to remain visible even when the system is on High Contrast mode. Is there code that can be entered that overrides the HC mode?
I've tried this on the Form Load event but no luck- no definition for graphics. (Not sure this would even be a viable solution):
OnPaint: e.Graphics.DrawImage(new Bitmap(BackgroundImage), 0, 0);
Aside from creating a PictureBox across my form and putting the image that way, does anyone know of a way to show the BG image of the form always?
Override the OnPaintBackground method:
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage(new Bitmap(BackgroundImage), e.ClipRectangle);
}
This DrawImage overload will stretch the image to fit the rectangle. If the ClipRectangle doesn't work (sorry, I can't test this right now!), create a new Rectangle with the background dimensions

Drawing a Grid of Dots on a PictureBox in 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);

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.

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