color blending using PathGradientBrush - c#

I have been stuck at this problem for few hours but can n't seem to make progress. I am trying to draw a rectangle which shows like the following
with a lightgrey border line. I am successfully able to draw the grey border line rectangle, but i am having trouble filling the rectangle.
There are 9 colors and i would like them to be space equally across the width of my rectangle.
My Code Looks like the following
using (var gp = new GraphicsPath())
{
gp.AddRectangle(rect);
using (var pgb = new PathGradientBrush(gp))
{
var pos = new[] { 0f, .125f, .25f, .375f, .5f, .625f, .75f, .875f, 1f };
var b = new Blend
{
Positions = pos,
Factors = new[] { .125f, .125f, .125f, .125f, .125f, .125f, .125f, .125f, .125f }
};
var cb = new ColorBlend
{
Positions = pos,
Colors = new[]
{
Color.FromArgb(0, 0, 0),
Color.FromArgb(0, 0, 79),
Color.FromArgb(81, 0, 123),
Color.FromArgb(152, 0, 118),
Color.FromArgb(211, 0, 62),
Color.FromArgb(245, 31, 0),
Color.FromArgb(255, 175, 0),
Color.FromArgb(255, 255, 100),
Color.FromArgb(255, 255, 255),
}
};
pgb.Blend = b;
pgb.InterpolationColors = cb;
pgb.CenterPoint = new PointF(rect.Width / 2, rect.Height / 2);
pgb.FocusScales = new PointF(.5f, .5f);
bufferedGraphics.Graphics.FillPath(pgb, gp);
}
}
but this ends up producing
Please ignore the inconsistent sizes of the two graphics.
Is this possible to do and if so how can i change my method to accomplish this ?

Instead of using the PathGradientBrush, use a LinearGradientBrush, because you want to draw the colors in a straight block. And (at least for me) it's easier to understand what happens.
I've tried the following code in a framework 2.0 project, s sorry for removing C#4.0 syntax.
using (LinearGradientBrush lgb = new LinearGradientBrush(rect, Color.Black, Color.White, 0f)) {
float[] pos = new float[] { 0f, .125f, .25f, .375f, .5f, .625f, .75f, .875f, 1f };
Blend b = new Blend();
b.Positions = pos;
b.Factors = new float[] { .125f, .125f, .125f, .125f, .125f, .125f, .125f, .125f, .125f };
ColorBlend cb = new ColorBlend();
cb.Positions = pos;
cb.Colors = new Color[] {
Color.FromArgb(0, 0, 0),
Color.FromArgb(0, 0, 79),
Color.FromArgb(81, 0, 123),
Color.FromArgb(152, 0, 118),
Color.FromArgb(211, 0, 62),
Color.FromArgb(245, 31, 0),
Color.FromArgb(255, 175, 0),
Color.FromArgb(255, 255, 100),
Color.FromArgb(255, 255, 255),
};
lgb.Blend = b;
lgb.InterpolationColors = cb;
e.Graphics.FillRectangle(lgb, rect);
}
Result:

Related

Cut the shapes out of the image with EMGU CV

All good times of day!
Faced with the following task: you need to find the shapes in the image, cut them out and save them in jpg or png.
Find the shapes worked out (attached the image), but how do I get their extreme coordinates?
I find shapes with EMGU CV.
I'm taking Image it out of PictureBox.
// - was like the idea to find the distance from the center to the edge of the figure through Moments, but did not understand how they work
Image<Gray, byte> grayImage = inputImage.SmoothMedian(1).Convert<Gray, byte>().ThresholdBinaryInv(new Gray(230), new Gray(255));
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Mat hierarchy = new Mat();
for (int i = 0; i < contours.Size; i++)
{
double perimetr = CvInvoke.ArcLength(contours[i], true);
VectorOfPoint approximation = new VectorOfPoint();
CvInvoke.ApproxPolyDP(contours[i], approximation, 0.04 * perimetr, true);
if (approximation.Size >= 3 && perimetr > 100 && approximation.Size != 5)
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
}
Moments moments = CvInvoke.Moments(contours[i]);
int x = (int)(moments.M10 / moments.M00);
int y = (int)(moments.M01 / moments.M00);
if (perimetr > 100)
{
if (approximation.Size == 3)
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Triangle", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
new MCvScalar(0, 0, 255), 1);
}
if (approximation.Size == 4)
{
Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
double aspectRatio = (double)rect.Width / (double)rect.Height;
if (aspectRatio >= 0.95 && aspectRatio <= 1.05)
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Square", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
new MCvScalar(0, 0, 255), 1);
}
else
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Rectangle", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
new MCvScalar(0, 0, 255), 1);
}
}
if (approximation.Size == 5)
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Pentagon", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
new MCvScalar(0, 0, 255), 1);
}
if (approximation.Size == 6)
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Hexagon", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
new MCvScalar(0, 0, 255), 1);
}
if (approximation.Size > 6)
{
var R = (double)perimetr / (3.14 * 2);
if (R > (Math.Min(inputImage.Width, inputImage.Height) / 3) * 0.1 && R < (Math.Min(inputImage.Width, inputImage.Height) / 2))
{
CvInvoke.DrawContours(inputImage, contours, i, new MCvScalar(0, 255, 0), 2);
CvInvoke.PutText(inputImage, "Circle", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyPlain, 1,
new MCvScalar(0, 0, 255), 1);
}
}
}
}
Thank you for your attention.
Input image
Output image
I found a solution to my question. There is a method that returns a description of the contour with an array of points.
Point[] contour = contours[i].ToArray();

Multi-color gradient does not show all colors

I have a multicolor gradient it consists of 14 colors. Here is my code:
panel1.Paint += new PaintEventHandler(panel1_Paint);
panel1.Refresh();
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 100, 100, 200),0, false);
System.Drawing.Drawing2D.ColorBlend cb = new System.Drawing.Drawing2D.ColorBlend();
cb.Positions = new[] { 0, 1 /15f, 2 / 15f, 3 / 15f, 4 / 15f, 5 / 15f, 6 / 15f, 7 / 15f, 8 / 15f, 9 / 15f, 10 / 15f, 11 / 15f, 12 / 15f, 13 / 15f,1 };
cb.Colors = new[] {
Color.FromArgb(255, 0, 0, 0),
Color.FromArgb(255, 0, 0, 50),
Color.FromArgb(255, 10, 10, 100),
Color.FromArgb(255, 30, 30, 100),
Color.FromArgb(255, 70, 70,200),
Color.FromArgb(255, 100, 100, 255),
Color.FromArgb(255, 170, 170, 255),
Color.FromArgb(255, 55, 151, 107),
Color.FromArgb(255, 117, 194, 103),
Color.FromArgb(255, 230, 230, 128),
Color.FromArgb(255, 202, 157, 75),
Color.FromArgb(255, 185, 154, 100),
Color.FromArgb(255, 220, 220, 220),
Color.FromArgb(255, 255, 255, 255),
Color.FromArgb(255, 100, 100, 200) };
br.InterpolationColors = cb;
// rotate
br.RotateTransform(90);
// paint
g.FillRectangle(br, this.ClientRectangle);
}
Unfortunately it does not show all the colors. My rectangle's height is 100 but it shows a gradient like this:
What is wrong and how can I fix it?
If you want to fill the Rectangle Vertical, you should specify the Rotation in the constructor of your LinearGradientBrush:
new LinearGradientBrush(this.ClientRectangle, colorFrom, colorTo, 90, false);
Here --^
And remove the Transformation
br.RotateTransform(90);
Since the width and height of the ClientRectangle are basically swapped by your current implementation (because of the 90° rotation), you either see only a small part of the gradient or the entire gradient multiple times.
This also doesn't work because you use the wrong ClientRectangle. You are using this.ClientRectangle in your Form's code-behind. Replace every this.ClientRectangle with panel1.ClientRectangle.
How this should look:
How your unmodified code looked:

Change image color with transparent background

I need to load an image with green circle over a transparent background into a bitmap image using c# (System.Drawings).
That's the easy part. However I need to change the color of the circle before adding it to the bigger image, without affecting the transparency of the surrounding. In my case I need to change the circle color to yellow and add it as a sun.
I can't use fixed yellow circle image because the desired color is dynamic.
So in the code below, how can I change the color of the image before adding it to the bitmap?
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//--> Here I need to change the color of the green circle to yellow
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
Please note that two things need to be into consideration: Keeping the anti-aliasing of the shape (circle), and the color needs to be picked by user and used as is to overlay the original color of the circle.
Fixed:
Thanks to #TaW, he provided the correct answer. However with a glitch, here's the final version that worked for me:
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//Here I need to change the color of the green circle to yellow
i = ChangeToColor(b, Color.Gold)
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
While ChangeToColor function is as follows:
Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R / 255f;
float tg = c.G / 255f;
float tb = c.B / 255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
This will create a new Bitmap with all non-transparent pixels moved strongly toward a new color:
Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R / 255f;
float tg = c.G / 255f;
float tb = c.B / 255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1} // kudos to OP!
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
do make sure not to leak the Bitmaps you create!
Note that there are other methods as well. Here is a link to a method that uses ColorMapping. This allows for a range of colors to be replaced by another range, so it can keep gradients like the ones you get in anti-alised graphics..
Here's my solution you just need to create a new Control
then inherit the Picturebox check this out.
public partial class UICirclePicture : PictureBox
{
[Browsable(false)]
public int Depth { get; set; }
[Browsable(false)]
public SprikiwikiUI Ui
{
get { return SprikiwikiUI.Instance; }
}
[Browsable(false)]
public MouseState MouseState { get; set; }
public UICirclePicture()
{
BackColor = Ui.GetApplicationBackgroundColor();
SizeMode = PictureBoxSizeMode.StretchImage;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
using (var gp = new GraphicsPath())
{
gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1));
this.Region = new Region(gp);
}
}
}

DrawImage c# How to change colors to black-white into the top left triangle?

Here is my code:
public static void ReduceScreenshot(string fileName)
{
var bmpSS = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
var gfxSS = Graphics.FromImage(bmpSS);
gfxSS.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] { 1.5f, 1.5f, 1.5f, 0, 0},
new float[] { 1.5f,1.5f, 1.5f, 0, 0},
new float[] {1.5f, 1.5f, 1.5f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {-1, -1, -1, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
Rectangle abc= new Rectangle(-783, -383, bmpSS.Width, bmpSS.Height);
gfxSS.DrawImage(bmpSS, abc, -783, -383, bmpSS.Width, bmpSS.Height, GraphicsUnit.Pixel, attributes);
bmpSS.Save("ScreenshotGray.png", ImageFormat.Png);
}
}
It's how it work http://take.ms/6tzvU. How should i change area from rectangle to triangle?
Not sure just which points shall set up the triangle but the code to restrict DrawImage to it should look something like this:
using System.Drawing.Imaging;
..
..
Rectangle abc = new Rectangle(-783, -383, bmpSS.Width, bmpSS.Height);
GraphicsPath gp = new GraphicsPath();
// three points to make up a triangle
// repeat the first one as one way of closing the path
// use your own coordinates!
Point[] p = new Point[] { new Point(12, 34), new Point(56, 78),
new Point(90, 12), new Point(12, 34) };
gp.AddLines(p); // or AddPolygon
gfxSS.SetClip(gp); // now restrict the Graphics object to the interior of the path
gfxSS.DrawImage(bmpSS, abc, -783, -383, bmpSS.Width, bmpSS.Height,
GraphicsUnit.Pixel, attributes);

how to fill C# Fill rectangle From bottom to top?

I have drawn vertical Rectangle but i want to fill it from bottom to top.
here is the code i used
System.Drawing.Drawing2D.LinearGradientBrush linGrBrush =
new System.Drawing.Drawing2D.LinearGradientBrush(
new Point(0, 12),
new Point(0,2),
Color.FromArgb(0, 0, 0, 0),
Color.FromArgb(255, 190, 0, 0));
Pen pen = new Pen(linGrBrush);
e.Graphics.FillRectangle(linGrBrush,groupBox2.Width -50,10, 25, x);
using (Pen Pen1 = new Pen(Color.BurlyWood, 4))
{
e.Graphics.DrawRectangle(Pen1, groupBox2.Width -50,10, 25, 200);
}
x is color fill offset which fill is used to fill rectangle after every 2 seconds.
I think that you should adjust the starting and the ending points of the gradient:
System.Drawing.Drawing2D.LinearGradientBrush linGrBrush =
new System.Drawing.Drawing2D.LinearGradientBrush(
new Point(0, 1),
new Point(0,0),
Color.FromArgb(0, 0, 0, 0),
Color.FromArgb(255, 190, 0, 0));

Categories

Resources