/// <summary> /// Add a filled rectangle /// </summary> /// <returns></returns> private void iAddFillRect(float x, float y, float width, float height, System.DrawingCore.Color c) { // Get the fill color _contentByte.SetRgbColorFill(c.R, c.G, c.B); _contentByte.Rectangle(x, PageSize.yHeight - y - height, width, height); _contentByte.Fill(); }
/// <summary> /// Sets a pixel at the x/y location to the new color. /// </summary> /// <param name="x">The x pixel location.</param> /// <param name="y">The y pixel location.</param> /// <param name="color">The color to set the pixel to.</param> public void SetPixel(int x, int y, Color color) { PixelData *pixel = (PixelData *)(_pBase + y * _width + x * sizeof(PixelData)); switch (_cFormat) { case ColorFormat.FormatABGR: pixel->a = color.A; pixel->r = color.B; pixel->g = color.G; pixel->b = color.R; break; case ColorFormat.FormatARGB: pixel->a = color.A; pixel->r = color.R; pixel->g = color.G; pixel->b = color.B; break; case ColorFormat.FormatBGRA: pixel->a = color.B; pixel->r = color.G; pixel->g = color.R; pixel->b = color.A; break; case ColorFormat.FormatRGBA: pixel->a = color.R; pixel->r = color.G; pixel->g = color.B; pixel->b = color.A; break; } }
/// <summary> /// Returns index of palette color closest to c /// 返回最接近C的调色板颜色索引 /// </summary> /// <param name="c">color</param> /// <returns>color index</returns> protected int FindClosest(Color c) { if (colorTab == null) { return(-1); } int r = c.R; int g = c.G; int b = c.B; int minpos = 0; int dmin = 256 * 256 * 256; int len = colorTab.Length; for (int i = 0; i < len;) { int dr = r - (colorTab[i++] & 0xff); int dg = g - (colorTab[i++] & 0xff); int db = b - (colorTab[i] & 0xff); int d = dr * dr + dg * dg + db * db; int index = i / 3; if (usedEntry[index] && (d < dmin)) { dmin = d; minpos = index; } i++; } return(minpos); }
/// <summary> /// Replaces the colors of a particular area: /// </summary> /// <param name="x">x</param> /// <param name="y">y</param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="newColor"></param> /// <returns></returns> public Rectangle FloodFill(int x, int y, Color newColor, int width = 0, int height = 0) { Color oldColor = GetPixel(x, y); if (ColorsEqual(oldColor, newColor)) { return(Rectangle.Empty); } Rectangle editRegion = new Rectangle(x, y, width, height); Stack <Point> points = new Stack <Point>(); points.Push(new Point(x, y)); SetPixel(x, y, newColor); while (points.Count > 0) { Point pt = points.Pop(); if (pt.X > 0) { CheckFloodPoint(points, pt.X - 1, pt.Y, oldColor, newColor); } if (pt.Y > 0) { CheckFloodPoint(points, pt.X, pt.Y - 1, oldColor, newColor); } if (pt.X < Width - 1) { CheckFloodPoint(points, pt.X + 1, pt.Y, oldColor, newColor); } if (pt.Y < Height - 1) { CheckFloodPoint(points, pt.X, pt.Y + 1, oldColor, newColor); } // grow the edit region: if (pt.X < editRegion.X) { editRegion.Width += editRegion.X - pt.X; editRegion.X = pt.X; } else if (pt.X > editRegion.Right) { editRegion.Width = pt.X - editRegion.X; } if (pt.Y < editRegion.Y) { editRegion.Height += editRegion.Y - pt.Y; editRegion.Y = pt.Y; } else if (pt.Y > editRegion.Bottom) { editRegion.Height = pt.Y - editRegion.Y; } } return(editRegion); }
private void CheckFloodPoint(Stack <Point> points, int x, int y, Color oldC, Color newC) { if (ColorsEqual(GetPixel(x, y), oldC)) { points.Push(new Point(x, y)); SetPixel(x, y, newC); } }
protected internal override void AddPolygon(PointF[] pts, StyleInfo si, string url) { if (si.BackgroundColor.IsEmpty) { return; // nothing to do } // Get the fill color - could be a gradient or pattern etc... System.DrawingCore.Color c = si.BackgroundColor; iAddPoints(pts); _contentByte.SetRgbColorFill(c.R, c.G, c.B); _contentByte.ClosePathFillStroke(); }
/// <summary> /// Replaces an old color with a new color. /// </summary> /// <param name="oldC">The color in the image to edit.</param> /// <param name="newC">The color to replace with.</param> public void ReplaceColor(Color oldC, Color newC) { for (int y = 0; y < Image.Height; ++y) { for (int x = 0; x < Image.Width; ++x) { if (ColorsEqual(GetPixel(x, y), oldC)) { SetPixel(x, y, newC); } } } }
/// <summary> /// 获取随机颜色 /// </summary> /// <returns></returns> private Color GetRandomColor() { Random RandomNum_First = new Random((int)DateTime.Now.Ticks); // 对于C#的随机数,没什么好说的 System.Threading.Thread.Sleep(RandomNum_First.Next(50)); Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks); // 为了在白色背景上显示,尽量生成深色 int int_Red = RandomNum_First.Next(256); int int_Green = RandomNum_Sencond.Next(256); int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green; int_Blue = (int_Blue > 255) ? 255 : int_Blue; return(Color.FromArgb(int_Red, int_Green, int_Blue)); }
/// <summary> /// 验证码图片 => byte[] /// </summary> /// <param name="verifyCode">验证码</param> /// <param name="width">宽</param> /// <param name="height">高</param> /// <returns>byte[]</returns> public byte[] CreateByteByImgCheckCode(string checkCode, int width, int height) { Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic)); Brush brush; Bitmap bitmap = new Bitmap(width, height); Graphics g = Graphics.FromImage(bitmap); SizeF totalSizeF = g.MeasureString(checkCode, font); SizeF curCharSizeF; PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2); Random random = new Random(); //随机数产生器 g.Clear(Color.White); //清空图片背景色 for (int i = 0; i < checkCode.Length; i++) { brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255))); g.DrawString(checkCode[i].ToString(), font, brush, startPointF); curCharSizeF = g.MeasureString(checkCode[i].ToString(), font); startPointF.X += curCharSizeF.Width; } //画图片的干扰线 for (int i = 0; i < 10; i++) { int x1 = random.Next(bitmap.Width); int x2 = random.Next(bitmap.Width); int y1 = random.Next(bitmap.Height); int y2 = random.Next(bitmap.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } //画图片的前景干扰点 for (int i = 0; i < 100; i++) { int x = random.Next(bitmap.Width); int y = random.Next(bitmap.Height); bitmap.SetPixel(x, y, Color.FromArgb(random.Next())); } g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线 g.Dispose(); //保存图片数据 MemoryStream stream = new MemoryStream(); bitmap.Save(stream, ImageFormat.Jpeg); //输出图片流 return(stream.ToArray()); }
/// <summary> /// Gets a pixel at the x/y location. /// </summary> /// <param name="x">The x pixel location.</param> /// <param name="y">The y pixel location.</param> /// <returns>The Color at the x/y location.</returns> /// <exception cref="Exception">Invalid color format.</exception> public Color GetPixel(int x, int y) { _pixelData = (PixelData *)(_pBase + y * _width + x * sizeof(PixelData)); switch (_cFormat) { case ColorFormat.FormatABGR: return(Color.FromArgb(_pixelData->a, _pixelData->b, _pixelData->g, _pixelData->r)); case ColorFormat.FormatARGB: return(Color.FromArgb(_pixelData->a, _pixelData->r, _pixelData->g, _pixelData->b)); case ColorFormat.FormatBGRA: return(Color.FromArgb(_pixelData->b, _pixelData->g, _pixelData->r, _pixelData->a)); case ColorFormat.FormatRGBA: return(Color.FromArgb(_pixelData->r, _pixelData->g, _pixelData->b, _pixelData->a)); } throw new Exception("Invalid color format."); }
/// <summary> /// Extracts image pixels into byte array "pixels" convert to correct format if necessary /// </summary> protected void GetImagePixels() { int w = image.Width; int h = image.Height; // int type = image.GetType().; if (w != Width || h != height) { // create new image with right size/format Image temp = new Bitmap(Width, height); Graphics g = Graphics.FromImage(temp); g.DrawImage(image, 0, 0); image = temp; g.Dispose(); } // ToDo:improve performance: use unsafe code try { pixels = new Byte[3 * image.Width * image.Height]; int count = 0; Bitmap tempBitmap = new Bitmap(image); for (int th = 0; th < image.Height; th++) { for (int tw = 0; tw < image.Width; tw++) { Color color = tempBitmap.GetPixel(tw, th); pixels[count] = color.R; count++; pixels[count] = color.G; count++; pixels[count] = color.B; count++; } } } catch { } //pixels = ((DataBufferByte)image.getRaster().getDataBuffer()).getData(); }
/// <summary> /// Page line element at the X Y to X2 Y2 position /// </summary> /// <returns></returns> internal void AddLine(float x, float y, float x2, float y2, float width, System.DrawingCore.Color c, BorderStyleEnum ls) { // Get the line color double red = c.R; double green = c.G; double blue = c.B; red = Math.Round((red / 255), 3); green = Math.Round((green / 255), 3); blue = Math.Round((blue / 255), 3); // Get the line style Dotted - Dashed - Solid string linestyle; switch (ls) { case BorderStyleEnum.Dashed: linestyle = "[3 2] 0 d"; break; case BorderStyleEnum.Dotted: linestyle = "[2] 0 d"; break; case BorderStyleEnum.Solid: default: linestyle = "[] 0 d"; break; } elements.AppendFormat(NumberFormatInfo.InvariantInfo, "\r\nq\t{0} w\t{1} {2} {3} RG\t{4}\t{5} {6} m\t{7} {8} l\tS\tQ\t", width, // line width red, green, blue, // line color linestyle, // line style x, pSize.yHeight - y, x2, pSize.yHeight - y2); // positioning }
protected internal override void AddLine(float x, float y, float x2, float y2, float width, System.DrawingCore.Color c, BorderStyleEnum ls) { // Get the line color _contentByte.SetRgbColorStroke(c.R, c.G, c.B); _contentByte.SetLineWidth(width); // Get the line style Dotted - Dashed - Solid switch (ls) { case BorderStyleEnum.Dashed: _contentByte.SetLineDash(new float[] { width * 3, width }, 0); break; case BorderStyleEnum.Dotted: _contentByte.SetLineDash(new float[] { width }, 0); break; case BorderStyleEnum.Solid: default: _contentByte.SetLineDash(new float[] { }, 0); break; } _contentByte.MoveTo(x, PageSize.yHeight - y); _contentByte.LineTo(x2, PageSize.yHeight - y2); _contentByte.Stroke(); }
public static string ConvertToHex(Color c) { return(ColorTranslator.ToHtml(CoreColor.FromArgb(c.ToArgb()))); }
/// <summary> /// 创建一个验证码 /// </summary> /// <returns></returns> public static VerifyCodeResult New() { Random random = new Random(); string code = null; for (int i = 0; i < 4; i++) { var index = random.Next(AllChars.Length - 1); code += AllChars[index]; } int codeW = 80; int codeH = 30; int fontSize = 16; //颜色列表,用于验证码、噪线、噪点 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue }; //字体列表,用于验证码 string[] font = { "Times New Roman" }; //验证码的字符集,去掉了一些容易混淆的字符 using (Bitmap bmp = new Bitmap(codeW, codeH)) { using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.White); //画噪线 for (int i = 0; i < 1; i++) { int x1 = random.Next(codeW); int y1 = random.Next(codeH); int x2 = random.Next(codeW); int y2 = random.Next(codeH); Color clr = color[random.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } //画验证码字符串 for (int i = 0; i < code.Length; i++) { string fnt = font[random.Next(font.Length)]; Font ft = new Font(fnt, fontSize); Color clr = color[random.Next(color.Length)]; g.DrawString(code[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0); } //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 using (MemoryStream ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Png); string id = Guid.NewGuid().ToString("N") + DateTimeOffset.Now.UtcTicks.ToString() + Guid.NewGuid().ToString("N"); return(new VerifyCodeResult { ImageData = ms.ToArray(), Str = code.ToLower(), Id = id }); } } } }
private static int GetGrayNumColor(System.DrawingCore.Color posClr) { return((posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16); }
/// <summary> /// 确定两种颜色是否相等。 /// </summary> /// <param name="col1">The color to compare.</param> /// <param name="col2">The color to compare against.</param> /// <returns>True if they field-wise match.</returns> public static bool ColorsEqual(Color col1, Color col2) => (col1.A == col2.A && col1.R == col2.R && col1.G == col2.G && col1.B == col2.B);
/// <summary> /// Sets the transparent color for the last added frame and any subsequent frames. /// * Since all colors are subject to modification /// * in the quantization process, the color in the final /// * palette for each frame closest to the given color /// * becomes the transparent color for that frame. /// * May be set to null to indicate no transparent color. /// </summary> /// <param name="c">c Color to be treated as transparent on display.</param> public void SetTransparent(Color c) { transparent = c; }
/// <summary> /// Creates an XColor structure from the specified alpha value and color. /// </summary> public static XColor FromArgb(int alpha, System.DrawingCore.Color color) { // Cast required to use correct constructor. return(new XColor((byte)alpha, color.R, color.G, color.B)); }
/// <summary> /// Creates an XColor structure from the specified System.DrawingCore.Color. /// </summary> public static XColor FromArgb(System.DrawingCore.Color color) { return(new XColor(color)); }
XColor(System.DrawingCore.Color color) : this(color.A, color.R, color.G, color.B) { }
internal abstract protected void AddLine(float x, float y, float x2, float y2, float width, System.DrawingCore.Color c, BorderStyleEnum ls);
protected virtual Color ConvertColor(ref Vector4 c) { return(Color.FromArgb(FloatToColor(c.X), FloatToColor(c.Y), FloatToColor(c.Z))); }