I want to make a bitmap that has a linear opacity applied. (i.e. it is more opaque on the left side and get progressively less opaque as it approaches the right.)
Is this possible? I know its possible to have a constant opacity level.
I know its possible to have a constant opacity level
So don't make it constant, LinearGradientBrush has no trouble interpolating the alpha value. A simple demonstration of a form that has the BackgroundImage set:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
var rc = new Rectangle(20, 20, this.ClientSize.Width - 40, 50);
using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
rc,
Color.FromArgb(255, Color.BlueViolet),
Color.FromArgb(0, Color.BlueViolet),
0f)) {
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
e.Graphics.FillRectangle(brush, rc);
}
}
Produced:
Put the BitMap into a PictureBox control. Then you will have to use the PictureBox's Paint event handler to do the fading. Something like
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(pictureBox.Image, 0, 0,
pictureBox.ClientRectangle, GraphicsUnit.Pixel);
Color left = Color.FromArgb(128, Color.Blue);
Color right = Color.FromArgb(128, Color.Red);
LinearGradientMode direction = LinearGradientMode.Horizontal;
LinearGradientBrush brush = new LinearGradientBrush(
pictureBox.ClientRectangle, left, right, direction);
e.Graphics.FillRectangle(brush, pictureBox.ClientRectangle);
}
This example fades the image overlay from blue to red. I am sure you can play with this to get what you want working.
I hope this helps.
Related
I am trying to draw a circle within my form.
But it is strange to me I set form width and height to a fixed number, i do the same for the circle, but the circle figure goes outside the form.
private void Form3_Paint(object sender, PaintEventArgs e)
{
this.SuspendLayout();
gr = this.CreateGraphics();
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush fill_circ3 = Brushes.Blue;
Pen ellipse_pen = new Pen(Color.Blue);
ellipse_pen.Width = (float)2.0;
this.Width = this.Height = 400;
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
gr.DrawEllipse(ellipse_pen, rect);
this.ResumeLayout();
}
The 3rd and 4th parameters of the Rectangle constructor defines the size in width and height of the circle.
See the circle I got
Why does the circle goes outside the form???! I have set form and circle sizes the same!!!
It's because you use the Window size, not the "client" size. Just replace your code by this:
gr.DrawEllipse(ellipse_pen, this.ClientRectangle);
The client area of a control is the bounds of the control, minus the
nonclient elements such as scroll bars, borders, title bars, and
menus.
How can I get the color of a pixel at the location of the cursor? I know how to get the mouses position using MousePosition but I can not figure out how to get the pixel color at that location. I write the code put I have no result when run
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
s= pictureBox1.PointToClient(Cursor.Position);
bitmap.SetPixel(s.X / 40, s.Y / 40, Color.Red);
}
It is easier to use the e.Location point in the parameter of the Mouseclick event:
Color c = ( (Bitmap)pictureBox1.Image).GetPixel(e.X, e.Y);
This assumes that indeed the bitmap is in the PicturBox's Image, not painted on top of the Control..
Make sure the event is actually hooked up!
To set a clicked pixel to, say red, you would get the Bitmap from the PB's Image and set the pixels, then move the Bitmap back in::
Bitmap bmp = (Bitmap)pictureBox1.Image;
bmp.SetPixel(e.X, e.Y, Color.Red);
pictureBox1.Image = bmp;
also in the MouseClick event.
If you want to get a larger mark you should use the Graphics methods, maybe like this:
using (Graphics G = Graphics.FromImage(pictureBox1.Image))
{
G.DrawEllipse(Pens.Red, e.X - 3, e.Y - 3, 6, 6);
}
Update: To combine getting and setting you could write:
Bitmap bmp = (Bitmap)pictureBox1.Image;
Color target = Color.FromArgb(255, 255, 255, 255);
Color c == bmp .GetPixel(e.X, e.Y);
if (c == target )
{
bmp.SetPixel(e.X, e.Y, Color.Red);
pictureBox1.Image = bmp;
}
else MessageBox.Show("Click only on white spots! You have hit " + c.ToString(),
"Wrong spot! ");
I have a panel on which I draw a radius frame with shadow. But I have a simple frame and a single color. How can I make her to be with smooth tints?
I think you need to use RGB and Alpha channel.
Color BorderColor = Color.FromArgb(((int)(((byte)(161)))), ((int)(((byte)(164)))), ((int)(((byte)(169)))));
private void AfyTextBox_Paint(object sender, PaintEventArgs e)
{
DrawShadow(e);
GraphicsPath path = RoundedRectangle.Create(0, 0, this.Width - 5, this.Height - 5, 3);
e.Graphics.DrawPath(new Pen(BorderColor), path);
}
I need to draw a rectangle into a panel. I dont know the color in advance, I get the color during runtime, I dont know how to set the color not to a fixed value, and second - when i try to draw the rectangle, it does nothing at all. Here is my code that should draw the rectangle (infact it does in another project, but thats just in a plain form, not into a panel)
Graphics g;
g = CreateGraphics();
Pen p;
Rectangle r;
p = new Pen(Brushes.Blue);
r = new Rectangle(1, 1, 578, 38);
g.DrawRectangle(p, r);`
So I need to replace (Brushes.Blue) with a variable and I need to draw the rectangle in a panel on its coordinates set in this code..
Construct your Pen using the Pen(Color) constructor instead of the Pen(Brush) one. Then you can define your color once you know it.
You should perform drawing in the Paint event of the panel. This event occurs whenever windows decides it's time to repaint the panel, and the PaintEventArgs contain a Graphics object you can draw the rectangle on.
The Brush is an abstract class, but you can use the SolidBrush object to create a custom colored brush at runtime:
int red = 255;
int green = 0;
int blue = 0;
Brush myBrush = new SolidBrush(Color.FromArgb(red, green, blue));
Here you are:
private Color _color; // save the color somewhere
private bool iKnowDaColor = false; // this will be set to true when we know the color
public Form1() {
InitializeComponents();
// on invalidate we want to be able to draw the rectangle
panel1.Paint += new PaintEventHandler(panel_Paint);
}
void panel_Paint(object sender, PaintEventArgs e) {
// if we know the color paint the rectangle
if(iKnowDaColor) {
e.Graphics.DrawRectangle(new Pen(_color),
1, 1, 578, 38);
}
}
And when you know the color:
_color = ...
iKnowDaColor = true;
// causes the panel to invalidate and our painting procedure to be called
panel.Invalidate();
I haven't tested this but should give you the basic idea.
put the following code in the appropriate place :
Graphics g = panel1.CreateGraphics();
int redInt=255, blueInt=255, greenInt=255; //255 is example, give it what u know
Pen p = new Pen(Color.FromArgb(redInt,blueInt,greenInt));
Rectangle r = new Rectangle(1, 1, 578, 38);
g.DrawRectangle(p, r);
and if you wanted to draw the rectangle somewhere else, say the form, you could do g = this.CreateGraphics.
I think that the better way of doing that is to extend the Panel class and add some custom OnPaint event logic.
public class PanelRect : Panel
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (Graphics g = e.Graphics)
{
Rectangle rect = ClientRectangle;
rect.Location = new Point(20, 20); // specify rectangle relative position here (relative to parent container)
rect.Size = new Size(30, 30); // specify rectangle size here
using (Brush brush = new SolidBrush(Color.Aqua)) // specify color here and brush type here
{
g.FillRectangle(brush, rect);
}
}
}
}
P.S. This is not an advanced example, but might help you. You can move size, location and color etc to properties so you can easily change them from designer.
P.S. P.S. If you need a non-filled rectangle just use Pen object instead of Brush (you might also change the FillRectangle to something more suitable).
I've been trying to mimic the Windows 7 Snipping Tool in how it overlays the screen with a semi-transparent gray layer which becomes completely transparent inside the selection area. I've come pretty close. I'm displaying a borderless 50% opaque gray form which covers the entire screen and has a TransparencyKey of Fuchsia. Then on top of that form I draw 2 rectangles. A solid fuchsia rectangle for the transparency and another rectangle for the red border. It works, but only if I do one of three things, none of which are options.
Disable double buffering which makes the form flicker while drawing
Change the desktop color mode to 16bit from 32bit
Make the form 100% opaque
Here is my code. Any suggestion on how to get this to work?
public partial class frmBackground : Form
{
Rectangle rect;
public frmBackground()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(frmBackground_MouseDown);
this.MouseMove += new MouseEventHandler(frmBackground_MouseMove);
this.Paint += new PaintEventHandler(frmBackground_Paint);
this.DoubleBuffered = true;
this.Cursor = Cursors.Cross;
}
private void frmBackground_MouseDown(object sender, MouseEventArgs e)
{
Bitmap backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
rect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
private void frmBackground_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
this.Invalidate();
}
private void frmBackground_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red, 3);
e.Graphics.DrawRectangle(pen, rect);
SolidBrush brush = new SolidBrush(Color.Fuchsia);
e.Graphics.FillRectangle(brush, rect);
}
}
you can use
Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue))
where alpha goes from 0 to 255, so a value of 128 for your alpha will give you 50% opactity.
this solution was found in this question