C# draw with Alpha channel - c#

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

Related

C# PrintPageEventArgs.Graphics.DrawRectangle prints incorrect size

I expected the code below to draw a rectangle 3 inches wide by 1 inch tall. Instead, the actual rectangle on the printed page is 4 inches wide by 2 inches tall.
Every rectangle I attempt to draw is always 1 inch wider and taller than I expect.
What am I doing wrong?
What addition properties in PrintPageEventArgs.Graphics should I be setting to get a correctly sized rectangle on the printed page?
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Inch;
e.Graphics.DrawRectangle(Pens.LightGray, 1, 1, 3, 1);
}
The code below fixed this issue. I suspect the default pen width is 1, which is interpreted as 1 inch. That made the rectangle extra big. Specifying a pen width of 0 fixed it.
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Inch;
Pen pen = new Pen(Color.LightGray, 0);
e.Graphics.DrawRectangle(pen, 1, 1, 3, 1);
}

C# GDI Rotate Projectile

I have this code
Graphics g;
g.FillRectangle(new SolidBrush(Color.Red), _Location.X - 2, _Location.Y - 2, 10, 10);
and the rectangle is shot at an angle to some direction, how can I get the rectangle to rotate while moving or rotate at all.
This should rotate a rectangle moving across the screen.
private int _angle = 0;
private Point _location = new Point(0, 0);
private void _timer_Tick(object sender, System.EventArgs e)
{
// nothing interesting here, moving the top left co-ordinate of the
// rectangle at constant rate
_location = new System.Drawing.Point(_location.X + 2, _location.Y + 2);
_angle += 5; // our current rotation angle
this.Invalidate();
}
void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// rebase the co-ordinate system so our current x,y is 0, 0 and that is
// the center of the rotation
g.TranslateTransform(_location.X, _location.Y, MatrixOrder.Append);
g.RotateTransform(_angle); // do the rotation
// make sure the centre of the rectangle is the centre of rotation (0, 0)
g.FillRectangle(new SolidBrush(Color.Red), -5, -5, 10, 10);
}
I've figured it out right before I saw #steve16351 's response, but his code was still useful. What I did was switch from using PointF to Rectangle, because the Rectangle has a property which holds the value of the upper left corner's coordinates, so I can increment/decrement that at a stable rate, in my game loop, to make it rotate.

mouse position and give the position color

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

Winforms C# Gradient Opacity bitmap

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.

c# / 2D Graphics / DrawRectangle size beheviour

private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Brushes.Black, 1);
e.Graphics.DrawRectangle(p, 3, 3, 89, 30);
}
Why does this code produce a 90px width / 31px height box? i.e one pixel bigger than requested.
BTW: FillRectangle works fine.
I imagine that it is because the command defines the inside size of the rectangle and draws the border around that.

Categories