Transparency key messing up on transparent images - c#

I have a C# WinForm that has a transparent key of Lime. I also set the background color of the form to Lime. This works great for things like buttons and almost anything you can think of. The form looks like it is not there and allows me to create a custom looking form.
I have a picture box with background of Transparent and the picture box image is has a drop shadow. When I run the application, the drop shadow is not transparent. The drow shadow has a background color of the forms background color (lime). It looks horrible.
How do I have a transparent form that also allows transparent images to be placed on it properly.
Programs such as SWTOR game launcher have this nice background drop shadow so I know it is possible.
Thank You!

You're looking for one of two things, either Windows Regions
OR
Layered Windows
Here is an excellent example for C#
Or a little example i put together:
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
GraphicsPath gp = new GraphicsPath();
Region r;
PointF[] p = new PointF[9];
p[0] = new PointF(70, 0);
p[1] = new PointF(170, 0);
p[2] = new PointF(240, 70);
p[3] = new PointF(240, 170);
p[4] = new PointF(170, 240);
p[5] = new PointF(70, 240);
p[6] = new PointF(0, 170);
p[7] = new PointF(0, 70);
p[8] = new PointF(70, 0);
gp.AddPolygon(p);
r = new Region(gp);
this.Region = r;
gp.Dispose();
r.Dispose();
}
you will need to put this code into the form you want it to effect.

Related

Can't get Control.OnPaint method to work

I couldnt find anything like that at all (basicly every problem from so else is always a syntax problem) and well..., the situation is a bit more complicated. to avoid using 500 lines of code im going to describe it for the most part:
Ive got a Form wich is acting as a parent Form (MdiParent) and another Form wich is a Child but basicly a fully functional Form at its own. Im using several OnPaint methods in the childform, witch work perfectly fine, and 3 custom buttons on the parent Form witch also have their own OnPaint methods. These 3 buttons (actualy panels) and every other control on the parent Form are contained in a PictureBox witch fills the parent Form completely and is used to make the background of the parent transparent / clickthrough via TransparencyKey (havnt found any other ways of doing that).
the Problem is that every OnPaint Method on the parent wont work at all (they're beeing executed but dont paint anything).
here is some code but that isnt the problem i'd say:
this.myButtonObject1.BackColor = System.Drawing.Color.Red;
this.myButtonObject1.Location = new System.Drawing.Point(840, 0);
this.myButtonObject1.Name = "myButtonObject1";
this.myButtonObject1.Size = new System.Drawing.Size(50, 50);
this.myButtonObject1.TabIndex = 0;
this.myButtonObject1.Click += new System.EventHandler(this.myButton1_Click);
this.myButtonObject1.Paint += new System.Windows.Forms.PaintEventHandler(this.myButtonObject1_Paint);
private void myButtonObject1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
LinearGradientBrush lgb = new LinearGradientBrush(new PointF(0, 0), new PointF(myButtonObject1.Width, myButtonObject1.Height), Color.Green, Color.Lime);
Pen p = new Pen(lgb, 5);
g.DrawRectangle(p, myButtonObject1.Bounds);
lgb.Dispose();
p.Dispose();
}
if anyone can tell me; what am i doing wrong?
PS: i m using .net 4.5, VS 2015, and havnt changed any of the default settings besides TopMost FormBorderStyle ShowInTaskbar StartPosition and ofc the color and trancparencyKey, but i dont think it has anything todo with that.
Update
The small error in your code is to use the Panel's Bounds property, which at runtime will refer to the Panel's Location within its Parent! But the drawing code must be relative to the object, not its parent!
So do not use Bounds but ClientRectangle and make sure to set the right PenAlignment:
using (LinearGradientBrush lgb =
new LinearGradientBrush(ClientRectangle, Color.Green, Color.Lime, 0f) ) //or some angle!
using (Pen p = new Pen(lgb, 5))
{
p.Alignment = PenAlignment.Inset;
g.DrawRectangle(p, ClientRectangle);
}
Set myButtonObject1.FlatStyle to FlatStyle.Standard.

Windows Forms button's ForeColor is not changing

I am using MDI Forms. In my parent form I have a ToolStrip with buttons. Yesterday the I had to change the text from black (which I think was the default color since I didn't change it before) to gray. I can see the text in the Designer changing to gray, in the MainForm.Designer.Cs the code is :
this.btnClients.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.btnClients.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.btnClients.ForeColor = System.Drawing.SystemColors.ActiveBorder;
this.btnClients.Image = global::ShoesUnlimitedAdmin.Properties.Resources.Clients;
this.btnClients.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.btnClients.ImageTransparentColor = System.Drawing.Color.Magenta;
this.btnClients.Name = "btnClients";
this.btnClients.Size = new System.Drawing.Size(93, 49);
this.btnClients.Text = "Clients";
this.btnClients.Click += new System.EventHandler(this.btnClients_Click);
And more exactly :
this.btnClients.ForeColor = System.Drawing.SystemColors.ActiveBorder;
which is kind of gray (I tried with custom color as well but didn't work either). But when I start the application the text color stays black as it was before.

Redrawing and custom shaped windows question

I'm using C#2.0 and I want to create a facebook style tooltip window. I currently made it with 2 windows and transparent key. One for the triangle arrow pointer and one for the square. The whole picture looks like that:
I have problem with the redrawing (as shown in the picture).
Is there a way to use whole shaped window on that? (While I need to make it sizeable)
If no, is this the proper way to make that? Or I need to 'glue' the triangle to the rectangle
Two ways to solve --
Using transparency: Irregular shaped Windows Form (C#)
Or using Control.Region which is the actual shaping of the window. Plenty of samples or:
How do I make a genuinely transparent Control?
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
const int ArrowSize = 25;
Point[] points = new[] {
new Point(ArrowSize, 0),
new Point(this.Width, 0),
new Point(this.Width, this.Height),
new Point(ArrowSize, this.Height),
new Point(ArrowSize, ArrowSize),
new Point(0, ArrowSize/2)
// don't need - autocloses
// ,new Point(ArrowSize, 0)
};
GraphicsPath path = new GraphicsPath();
path.AddLines(points);
this.Region = new Region(path);
}

C# Forms Picturebox to show a solid color instead of an image

Im making a little app to display the pictures of guests as they scan their cards.
But i want to to display blank green or red (green if the guest exists without a photo and red if they dont exist)
But i cant figure out how to create a blank colour image.
Bitmap bmRed = new Bitmap(imgbox.Width, imgbox.Height, PixelFormat.Format24bppRgb);
imgbox.Image = bmRed;
Thats the code i have at the moment and it just makes the box black.
imgbox is a PictureBox
Don't use an image - set the BackColor property of the PictureBox:
imgBox.BackColor = Color.Red;
To prevent null pointer exception, create a blank bmp
myPicBox.Image = new Bitmap(myPicBox.Width, myPicBox.Height);
Graphics graphics = Graphics.FromImage(myPicBox.Image);
Brush brush = new SolidBrush(Color.Gray);
graphics.FillRectangle(brush, new System.Drawing.Rectangle(0, 0, myPicBox.Width, myPicBox.Height));
How about setting the background color directly?
imgbox.BackColor = Color.Red;
create a graphics context and draw using it.
using(Graphics g = Graphics.FromImage(bmRed))
{
g.FillRectangle(new SolidBrush(Color.Red),0,0,imgbox.Width,imgbox.Height);
}
Single statement:
Graphics.FromImage(PicBox.Image=new bitmap(PicBox.Size)).FillRectangle(Brushes.Red,new Rectangle (Point.EMPTY,PicBox.Size));

How do I display an image within a region defined in a Picturebox?

As a follow on to my previous question I have gotten my region but spent the last two hours trying to display tiny pictures wihing that region alone; with the end goal being to be able to arbitarily display any number of images I choose.
so far this is my code:
void OnPaintRadar(Object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Bitmap blip = new Bitmap(tst_Graphics.Properties.Resources.dogtag);
Rectangle radar_rect = new Rectangle(myRadarBox.Left + 80, myRadarBox.Left + 7, myRadarBox.Width - 200, myRadarBox.Height + 200);
using (Pen drw_pen = new Pen(Color.White, 1) )
{
using (GraphicsPath path = new GraphicsPath())
{
path.AddPie(radar_rect, 180, 180);
path.CloseFigure();
using (Region rdRegion = new Region(path) )
{
g.DrawPath(drw_pen, path);
g.Clip = rdRegion;
PictureBox pb = new PictureBox();
pb.Image = (blip);
pb.Size = blip.Size;
g.DrawImage(blip, radar_rect);
}
}
}
}// end paint method
I have tried the DrawImageUnscaled method also but I either get a my tiny picture blown to fill the pie region or nothing is displayed.
Click here to run a sample application that demonstrates the basics of how to do radar (or one way, at least). Note: this application does not do double-buffering or transparency of the tiny image.
Source code for the project is here.
This line:
pb.Image = (blip);
is what's causing the tiny image to appear large. Basically, you pulling a tiny bitmap out of resources, and then setting the PictureBox's Image property to that bitmap (I'm assuming "pb" is a PictureBox on your form). Try commenting out that line and the line below it.

Categories

Resources