public static void Remove(int colour, ref Bitmap bp) { Color c; switch (colour) { case BLUE: for (int i = 1; i < bp.Width; i++) for (int j = 1; j < bp.Height; j++) { c = bp.GetPixel(i, j); bp.SetPixel(i, j, Color.FromArgb(c.R, c.G, 0)); } break; case RED: for (int i = 1; i < bp.Width; i++) for (int j = 1; j < bp.Height; j++) { c = bp.GetPixel(i, j); bp.SetPixel(i, j, Color.FromArgb(0, c.G, c.B)); } break; case GREEN: for (int i = 1; i < bp.Width; i++) for (int j = 1; j < bp.Height; j++) { c = bp.GetPixel(i, j); bp.SetPixel(i, j, Color.FromArgb(c.R, 0, c.B)); } break; } }
// From http://edu.cnzz.cn/show_3281.html public static GraphicsPath CalculateGraphicsPathFromBitmap(Bitmap bitmap, Color colorTransparent) { GraphicsPath graphicsPath = new GraphicsPath(); if (colorTransparent == Color.Empty) colorTransparent = bitmap.GetPixel(0, 0); for(int row = 0; row < bitmap.Height; row ++) { int colOpaquePixel = 0; for(int col = 0; col < bitmap.Width; col ++) { if(bitmap.GetPixel(col, row) != colorTransparent) { colOpaquePixel = col; int colNext = col; for(colNext = colOpaquePixel; colNext < bitmap.Width; colNext ++) if(bitmap.GetPixel(colNext, row) == colorTransparent) break; graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1)); col = colNext; } } } return graphicsPath; }
public Bitmap Binariz(Bitmap bm) { int x = bm.Width; int y = bm.Height; Bitmap result = new Bitmap(x, y); for (int pixX = 0; pixX < x; pixX++) for (int pixY = 0; pixY < y; pixY++) { int rd, gr, bl; if (bm.GetPixel(pixX, pixY).R < 100) rd = 0; else rd = 255; if (bm.GetPixel(pixX, pixY).G < 100) gr = 0; else gr = 255; if (bm.GetPixel(pixX, pixY).B < 100) bl = 0; else bl = 255; result.SetPixel(pixX, pixY, Color.FromArgb((byte)rd, (byte)gr, (byte)bl)); } return result; }
public Bitmap Filter(Bitmap originalBitmap) { Bitmap resultBitmap = new Bitmap(originalBitmap); Color color; for (int i = SIZE; i < originalBitmap.Width - SIZE; i++) { for (int j = SIZE; j < originalBitmap.Height - SIZE; j++) { List<int> redList = new List<int>(); List<int> greenList = new List<int>(); List<int> blueList = new List<int>(); int red = 0, green = 0, blue = 0; for (int ii = -SIZE; ii < SIZE + 1; ii++) { for (int jj = -SIZE; jj < SIZE + 1; jj++) { color = originalBitmap.GetPixel(i + ii, j + jj); redList.Add(color.R); greenList.Add(color.G); blueList.Add(color.B); } } redList.Sort(); greenList.Sort(); blueList.Sort(); red = redList.ElementAt(SIZE); green = greenList.ElementAt(SIZE); blue = blueList.ElementAt(SIZE); color = originalBitmap.GetPixel(i, j); color = Color.FromArgb(color.A, FilterService.SetColor(red), FilterService.SetColor(green), FilterService.SetColor(blue)); resultBitmap.SetPixel(i, j, color); } } return resultBitmap; }
public static IntegralImage FromImage(Bitmap image) { IntegralImage pic = new IntegralImage(image.Width, image.Height); float rowsum = 0; for (int x = 0; x < image.Width; x++) { Color c = image.GetPixel(x, 0); rowsum += (cR * c.R + cG * c.G + cB * c.B) / 255f; pic[0, x] = rowsum; } for (int y = 1; y < image.Height; y++) { rowsum = 0; for (int x = 0; x < image.Width; x++) { Color c = image.GetPixel(x, y); rowsum += (cR * c.R + cG * c.G + cB * c.B) / 255f; // integral image is rowsum + value above pic[y, x] = rowsum + pic[y - 1, x]; } } return pic; }
private static bool Findline(ref int linepointx, ref int linepointy, Bitmap bimage) { byte begincolor = (byte)bimage.GetPixel(linepointx - WINDOWSIZE / 2, linepointy - WINDOWSIZE / 2).ToArgb(); for (int i = 0; i < WINDOWSIZE; i++) { int yi = linepointy + i - WINDOWSIZE / 2; for (int j = 0; j < WINDOWSIZE; j++) { int xj = linepointx + j - WINDOWSIZE / 2; byte curr = (byte)bimage.GetPixel(xj, yi).ToArgb(); if (Math.Abs(begincolor - curr) > 100) { linepointx = xj; linepointy = yi; if (curr < 120) //line (black) found { if (j == 0) linepointy--; else linepointx--; } return true; } } } return false; }
/// <summary> /// Save source bitmap image as text which contains all positions' pixel value /// random position value = {R, G, B} /// </summary> /// <param name="sourceImage"></param> /// <param name="path"></param> public static void write_Bmp_To_RGB_Number(Bitmap sourceImage, string path) { try { int i = 0, j = 0; StreamWriter sw = new StreamWriter(path); for (j = 0; j < sourceImage.Height; j++) { for (i = 0; i < sourceImage.Width; i++) { sw.Write("({0},{1},{2}) ", sourceImage.GetPixel(i, j).R.ToString().PadLeft(3, '0').Replace("255", " "), sourceImage.GetPixel(i, j).G.ToString().PadLeft(3, '0').Replace("255", " "), sourceImage.GetPixel(i, j).B.ToString().PadLeft(3, '0').Replace("255", " ")); } sw.WriteLine(); } sw.Flush(); sw.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } }
public PSNR(Bitmap hostImage, Bitmap outputImage) { int m = outputImage.Width; int n = outputImage.Height; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { r.sum += Math.Pow((double)(hostImage.GetPixel(i, j).R - outputImage.GetPixel(i, j).R), 2.0); g.sum += Math.Pow((double)(hostImage.GetPixel(i, j).G - outputImage.GetPixel(i, j).G), 2.0); b.sum += Math.Pow((double)(hostImage.GetPixel(i, j).B - outputImage.GetPixel(i, j).B), 2.0); gray.sum += Math.Pow((double)(ConvertGray(hostImage.GetPixel(i, j)) - ConvertGray(outputImage.GetPixel(i, j))), 2.0); } } r.MSE = Math.Round(r.sum / (double)(m * n), 2); g.MSE = Math.Round(g.sum / (double)(m * n), 2); b.MSE = Math.Round(b.sum / (double)(m * n), 2); gray.MSE = Math.Round(gray.sum / (double)(m * n), 2); //r.PSNR = Math.Round(10 * (Math.Log10(((2 * (m * n)) - 1) * 2 / r.MSE)), 2); //g.PSNR = Math.Round(10 * (Math.Log10(((2 * (m * n)) - 1) * 2 / g.MSE)), 2); //b.PSNR = Math.Round(20 * (Math.Log10(((2 * (m * n)) - 1) * 2 / b.MSE)), 2); //gray.PSNR = Math.Round(10 * (Math.Log10(((2 * (m * n)) - 1) * 2 / gray.MSE)), 2); r.PSNR = Math.Round(20 * Math.Log10(255) - 10 * Math.Log10(r.MSE), 2); g.PSNR = Math.Round(20 * Math.Log10(255) - 10 * Math.Log10(g.MSE), 2); b.PSNR = Math.Round(20 * Math.Log10(255) - 10 * Math.Log10(b.MSE), 2); gray.PSNR = Math.Round(20 * Math.Log10(255) - 10 * Math.Log10(gray.MSE), 2); }
private static Bitmap Scan(Bitmap pic, int starx, int starty, int cx, int cy, bool[] scanned, Color bk) { int x0 = starx, x1 = starx, y0 = starty, y1 = starty; Stack<Point> pts = new Stack<Point>(); pts.Push(new Point(starx, starty)); while (pts.Count != 0) { var pt = pts.Pop(); var fg = pic.GetPixel(pt.X, pt.Y); if (fg == bk) continue; x0 = Math.Min(x0, pt.X); x1 = Math.Max(x1, pt.X); y0 = Math.Min(y0, pt.Y); y1 = Math.Max(y1, pt.Y); if (CanMove2(pt.X - 1, pt.Y, cx, cy, scanned)) { pts.Push(new Point(pt.X - 1, pt.Y)); } if (CanMove2(pt.X + 1, pt.Y, cx, cy, scanned)) { pts.Push(new Point(pt.X + 1, pt.Y)); } if (CanMove2(pt.X, pt.Y - 1, cx, cy, scanned)) { pts.Push(new Point(pt.X, pt.Y - 1)); } if (CanMove2(pt.X, pt.Y + 1, cx, cy, scanned)) { pts.Push(new Point(pt.X, pt.Y + 1)); } } int px = x1 - x0 + 1, py = y1 - y0 + 1; if (px < 2 || py < 2) return null; Bitmap p2 = new Bitmap(px, py); for (int a = 0; a < px; a++) { for (int b = 0; b < py; b++) { p2.SetPixel(a, b, pic.GetPixel(x0 + a, y0 + b)); } } return p2; }
private static Image AmazonCut(Image image) { if (image.Width != image.Height) return image; var bmp = new Bitmap(image); int size = image.Height; int white = System.Drawing.Color.FromKnownColor(KnownColor.White).ToArgb(); int i = 0; while (i < size/2) { if (bmp.GetPixel(i, i).ToArgb() != white) break; if (bmp.GetPixel(i, size - 1 - i).ToArgb() != white) break; if (bmp.GetPixel(size - 1 - i, i).ToArgb() != white) break; if (bmp.GetPixel(size - 1 - i, size - 1 - i).ToArgb() != white) break; i++; } if (i > 0) { i += 8; var zone = new Rectangle(i, i, size - 2*i, size - 2*i); return bmp.Clone(zone, System.Drawing.Imaging.PixelFormat.DontCare); } return bmp; }
public override void UpdateDevice(Bitmap bitmap) { if (!CanUse || bitmap == null) return; if (bitmap.Width != bitmap.Height) throw new ArgumentException("Bitmap must be a perfect square"); var leds = CueSDK.HeadsetSDK.Leds.Count(); var step = (double) bitmap.Width/leds; using (bitmap) { var ledIndex = 0; // Color each LED according to one of the pixels foreach (var corsairLed in CueSDK.HeadsetSDK.Leds) { var col = ledIndex == 0 ? bitmap.GetPixel(0, 0) : bitmap.GetPixel((int) ((ledIndex + 1)*step - 1), (int) ((ledIndex + 1)*step - 1)); corsairLed.Color = col; ledIndex++; } } CueSDK.HeadsetSDK.Update(); }
//*************改变亮度和对比度 public void AddDiff(ref Bitmap src) { List<int> RGB = new List<int>(); RGB.Add(0); RGB.Add(0); RGB.Add(0); for (int i = 0; i < src.Width; i++) { for (int j = 0; j < src.Height; j++) { Color clr = src.GetPixel(i, j); RGB[0] += clr.R; RGB[1] += clr.G; RGB[2] += clr.B; } } RGB[0] = RGB[0] / (src.Height * src.Width); RGB[1] = RGB[1] / (src.Height * src.Width); RGB[2] = RGB[2] / (src.Height * src.Width); for (int i = 0; i < src.Width; i++) { for (int j = 0; j < src.Height; j++) { int r, g, b; Color clr = src.GetPixel(i, j); r = clr.R; g = clr.G; b = clr.B; r -= RGB[0]; g -= RGB[1]; b -= RGB[2]; r = (int)(r * 4); g = (int)(g * 4); b = (int)(b * 4); r += (int)(RGB[0] * 2); g += (int)(RGB[1] * 2); b += (int)(RGB[2] * 2); r = r < 0 ? 0 : r; g = g < 0 ? 0 : g; b = b < 0 ? 0 : b; r = r > 255 ? 255 : r; g = g >255 ? 255 : g; b = b >255 ? 255 : b; src.SetPixel(i, j, Color.FromArgb(r, g, b)); } } }
public void testEdgeDetection() { String testPath = "testEdge/"; String[] pictures = Directory.GetFiles(testPath); EdgeDetector eD = new EdgeDetector(testPath); eD.execute(); String directory = "Edged/"; String[] edgedPictures = Directory.GetFiles(directory); Assert.AreEqual(edgedPictures.Length, pictures.Length, "Some images were not preccessed in Edge Detection"); int edgedCount = 0; for (int i = 0; i < edgedPictures.Length; i++) { bool isBlackOrWhite = true; Bitmap finalImages = new Bitmap(edgedPictures[i]); for (int j = 0; j < finalImages.Width; j++) { for (int k = 0; k < finalImages.Height; k++) { if(finalImages.GetPixel(j,k).R > 0 && finalImages.GetPixel(j,k).R < 255) { isBlackOrWhite = false; break; } } if(!isBlackOrWhite) break; } if (isBlackOrWhite) edgedCount++; Assert.AreEqual(true, isBlackOrWhite, "Image is not an edged image"); } Assert.AreEqual(edgedPictures.Length, edgedCount, "Not all images were Edged"); }
private bool IsEmptyBitmap (Bitmap bitmap, out int x, out int y) { bool result = true; int empty = Color.Empty.ToArgb (); #if false for (y = 0; y < bitmap.Height; y++) { for (x = 0; x < bitmap.Width; x++) { if (bitmap.GetPixel (x, y).ToArgb () != empty) { Console.Write ("X"); result = false; } else Console.Write (" "); } Console.WriteLine (); } #else for (y = 0; y < bitmap.Height; y++) { for (x = 0; x < bitmap.Width; x++) { if (bitmap.GetPixel (x, y).ToArgb () != empty) return false; } } #endif x = -1; y = -1; return result; }
private void binarize(Bitmap bittt, ref int[,] massss, ToolStripProgressBar progress) { massss = new int[bittt.Width, bittt.Height]; progress.Minimum = 0; progress.Maximum = bittt.Height; for (int i = 0; i < bittt.Height; i++) { for (int j = 0; j < bittt.Width; j++) { if (bittt.GetPixel(j, i).R > 200 || bittt.GetPixel(j, i).B > 200 || bittt.GetPixel(j, i).G > 200) { bittt.SetPixel(j, i, Color.White); massss[j, i] = 0; } else { bittt.SetPixel(j, i, Color.Black); massss[j, i] = 1; } /* if (j == (int)(bittt.Height / 6)) { bittt.SetPixel(j, i, Color.Teal); }*/ } if (progress.Value < bittt.Height) { progress.Value++; } } progress.Value = 0; }
/// <summary> /// カーソルを作成する /// </summary> /// <param name="bitmap">カーソル画像</param> /// <param name="xHotSpot">ホットスポットのX座標</param> /// <param name="yHotSpot">ホットスポットのY座標</param> /// <returns>作成したカーソル</returns> public static Cursor CreateCursor(Bitmap bitmap, int xHotSpot, int yHotSpot) { Bitmap andMask = new Bitmap(bitmap.Width, bitmap.Height); Bitmap xorMask = new Bitmap(bitmap.Width, bitmap.Height); Color transparent = bitmap.GetPixel(bitmap.Width - 1, bitmap.Height - 1); for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { Color pixel = bitmap.GetPixel(x, y); if (pixel == transparent) { andMask.SetPixel(x, y, Color.White); xorMask.SetPixel(x, y, Color.Transparent); } else { andMask.SetPixel(x, y, Color.Black); xorMask.SetPixel(x, y, pixel); } } } IntPtr hIcon = bitmap.GetHicon(); IconInfo info = new IconInfo(); NativeMethods.GetIconInfo(hIcon, ref info); info.xHotspot = xHotSpot; info.yHotspot = yHotSpot; info.hbmMask = andMask.GetHbitmap(); info.hbmColor = xorMask.GetHbitmap(); info.fIcon = false; hIcon = NativeMethods.CreateIconIndirect(ref info); return new Cursor(hIcon); }
//метод простой бинаризации, пока не используется, в дальнейшем - с помощью него будет происходить бинар-я с заданным порогом public Bitmap Filter(Bitmap originalBitmap) { int treshold = 0; // порог Color color; // цвет - будет разбиваться на RGB int average = 0; // средний цвет = яркость Bitmap resultBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height); for (int i = 0; i < originalBitmap.Width; i++) { for (int j = 0; j < originalBitmap.Height; j++) { color = originalBitmap.GetPixel(i, j); average = (int)(color.R + color.G + color.B) / 3; treshold = treshold + average; } } treshold = treshold / (originalBitmap.Width * originalBitmap.Height); for (int i = 0; i < originalBitmap.Width; i++) { for (int j = 0; j < originalBitmap.Height; j++) { color = originalBitmap.GetPixel(i, j); average = (int)(color.R + color.G + color.B) / 3; resultBitmap.SetPixel(i, j, average < treshold ? Color.Black : Color.White); } } return resultBitmap; }
public static byte[] Convert(Bitmap img, byte mul, byte water, int size) { var data = new byte[size * size * 13]; var i = 0; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { var color = img.GetPixel(x, y); int c = (color.R + color.G + color.B) / 3; img.GetPixel(x, y); data[i++] = System.Convert.ToByte(c); data[i++] = mul; data[i++] = water; for (int j = 0; j < 10; j++) { data[i++] = 0; } } } return data; }
public void Can_Create_Color_Image() { var bitmap = new Bitmap(20, 10); var imageFactory = new RgbBitmapFactory(); var pointerFactory = new ArrayToPointerFactory(); var data = new ushort[20 * 10 * 2]; for (int index = data.Length / 2; index < data.Length; index++) { data[index] = ushort.MaxValue; } var pointer = pointerFactory.CreatePointer(data); try { imageFactory.CreateImage(bitmap, pointer); } finally { pointerFactory.Destroy(pointer); } var color = bitmap.GetPixel(0, 0); var color2 = bitmap.GetPixel(10, 9); Assert.AreEqual(0, color.R); Assert.AreEqual(0, color.G); Assert.AreEqual(0, color.B); Assert.AreEqual(255, color2.R); Assert.AreEqual(255, color2.G); Assert.AreEqual(255, color2.B); }
public static int[] convertToTXT(Bitmap img) { double averageBrightness = 0; for (int y = 0; y < img.Height; y++) { for (int x = 0; x < img.Width; x++) { averageBrightness += img.GetPixel(x, y).R; } } averageBrightness /= (img.Width * img.Height); int[] result = new int[img.Width * img.Height]; for (int y = 0; y < img.Height; y++) { for (int x = 0; x < img.Width; x++) { if (img.GetPixel(x, y).R < averageBrightness) result[img.Width * y + x] = 0; else result[img.Width * y + x] = 1; } } return result; }
public void GenerateBackgroundTest() { Bitmap backgroundTemplate = new Bitmap(30, 20); backgroundTemplate.SetPixel(0, 0, Color.Red); backgroundTemplate.SetPixel(19, 9, Color.Blue); const int width = 50; const int height = 50; Bitmap actual = StereoPictureBuilder_Accessor.GenerateBackground(backgroundTemplate, width, height); Bitmap expected = new Bitmap(width, height); expected.SetPixel(0, 0, Color.Red); expected.SetPixel(30, 0, Color.Red); expected.SetPixel(0, 20, Color.Red); expected.SetPixel(30, 20, Color.Red); expected.SetPixel(19, 9, Color.Blue); expected.SetPixel(49, 9, Color.Blue); expected.SetPixel(19, 29, Color.Blue); expected.SetPixel(49, 29, Color.Blue); expected.SetPixel(19, 49, Color.Blue); expected.SetPixel(49, 49, Color.Blue); //Assert.AreEqual(expected, actual); Assert.AreEqual(expected.Width, actual.Width); Assert.AreEqual(expected.Height, actual.Height); Assert.AreEqual(expected.GetPixel(0, 0), actual.GetPixel(0, 0)); Assert.AreEqual(expected.GetPixel(49, 49), actual.GetPixel(49, 49)); }
public static int[][] BitmapToDoubleArray(Bitmap fileBitmap, string extension) { int[][] uploadedDocument; int width = fileBitmap.Width; int height = fileBitmap.Height; uploadedDocument = new int[width][]; for (int i = 0; i < width; i++) uploadedDocument[i] = new int[height]; Color pixelColor; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { switch(extension){ case ".bmp": pixelColor = fileBitmap.GetPixel(i, j); uploadedDocument[i][j] = (int)(pixelColor.R * 0.3 + pixelColor.G * 0.59 + pixelColor.B * 0.11); break; case ".png": uploadedDocument[i][j] = (255 - (int)fileBitmap.GetPixel(i,j).A); break; } } } return uploadedDocument; }
public static byte[] Compare(/*Bitmap _first, Bitmap _second*/) { Bitmap first = new Bitmap(@"C:\Users\netstalkerrr\Documents\Visual Studio 2013\Projects\testAppImage\img\1.png"); Bitmap second = new Bitmap(@"C:\Users\netstalkerrr\Documents\Visual Studio 2013\Projects\testAppImage\img\2.png"); Bitmap difference = new Bitmap(first.Width, first.Height); for (int i = 0; i < first.Width; i++) { for (int j = 0; j < first.Height; j++) { int r1 = second.GetPixel(i, j).R; int g1 = second.GetPixel(i, j).G; int b1 = second.GetPixel(i, j).B; int r2 = first.GetPixel(i, j).R; int g2 = first.GetPixel(i, j).G; int b2 = first.GetPixel(i, j).B; if (r1 != r2 && g1 != g2 && b1 != b2) { difference.SetPixel(i, j, Color.Red); } else difference.SetPixel(i, j, first.GetPixel(i, j)); } } var bitmapBytes = BitmapToBytes(difference); return bitmapBytes; }
private void button2_Click(object sender, EventArgs e) { diff = (int)this.numericUpDown1.Value; Bitmap sourceBmp = new Bitmap(this.pictureBox1.Image); Bitmap targetBmp = new Bitmap(sourceWidth, sourceHeight); for (int x = 0; x < sourceWidth; x++) { for (int y = 0; y < sourceHeight; y++) { Color curColor = sourceBmp.GetPixel(x, y); List<Point> testPoints = new List<Point>(8); //testPoints.Add(new Point(x - 1, y - 1)); //testPoints.Add(new Point(x, y - 1)); testPoints.Add(new Point(x + 1, y - 1)); //testPoints.Add(new Point(x, y)); testPoints.Add(new Point(x + 1, y)); //testPoints.Add(new Point(x - 1, y + 1)); testPoints.Add(new Point(x, y + 1)); testPoints.Add(new Point(x + 1, y + 1)); foreach (Point p in testPoints) { if (InsideImg(p)) if (IsBeyondDiff(sourceBmp.GetPixel(p.X, p.Y), curColor)) { targetBmp.SetPixel(x, y, Color.Black); break; } } } } this.pictureBox2.Image = targetBmp; }
public Stroke(Bitmap bitmap,Color color,float a,float b,float c,float soft,bool flat,bool square) { //Create shape shape=(Bitmap)bitmap.Clone(); for(int i=0;i<shape.Width;i++) { for(int j=0;j<shape.Height;j++) { float x=(float)(i-shape.Width/2); float y=(float)(j-shape.Height/2); float len2=x*x+y*y; float r=Math.Min(shape.Width/2,shape.Height/2); float r2=r*r; if(square||len2<=r2) { Color col=shape.GetPixel(i,j); float ii=(((float)(col.R+col.G+col.B))/3f)/255f; int aa=(int)(((a*ii*ii+b*ii+c)*(((float)color.A)/255f)*255f)); if(aa<0) aa=0; if(aa>255) aa=255; float sa=1f; if(soft>0f) { sa=1f-(len2/r2)*soft; if(sa<0f) sa=0f; if(sa>1f) sa=1f; } if(flat) shape.SetPixel(i,j,Color.FromArgb((int)(((float)aa)*sa),color)); else { Color cc=shape.GetPixel(i,j); int rr=(int)((((float)cc.R)/255f)*(((float)color.R)/255f)*255f); if(rr<0) rr=0; if(rr>255) rr=255; int gg=(int)((((float)cc.G)/255f)*(((float)color.G)/255f)*255f); if(gg<0) gg=0; if(gg>255) gg=255; int bb=(int)((((float)cc.B)/255f)*(((float)color.B)/255f)*255f); if(bb<0) bb=0; if(bb>255) bb=255; shape.SetPixel(i,j,Color.FromArgb((int)(((float)aa)*sa),rr,gg,bb)); } } else shape.SetPixel(i,j,Color.FromArgb(0,0,0,0)); } } }
public static Bitmap PopularityQuantization(Bitmap bmp, int colorsNr) { Dictionary<Color, int> colorDictionary = new Dictionary<Color, int>(); Color color; Bitmap result = new Bitmap(bmp.Width, bmp.Height); for(int i = 0; i < bmp.Width; ++i) for (int j = 0; j < bmp.Height; ++j) { color = bmp.GetPixel(i, j); if (!colorDictionary.ContainsKey(color)) colorDictionary.Add(color, 0); ++colorDictionary[color]; } KeyValuePair<Color, int>[] sortedColorsByFrequency = colorDictionary.OrderByDescending(f => f.Value).Take(colorsNr).ToArray(); Color[] availableColors = new Color[colorsNr]; int k = -1; foreach(var pair in sortedColorsByFrequency) availableColors[++k] = pair.Key; for (int i = 0; i < bmp.Width; ++i) for (int j = 0; j < bmp.Height; ++j) { color = FindClosestColor(availableColors, bmp.GetPixel(i, j)); result.SetPixel(i, j, color); } return result; }
private Task LoadFavs() { return Task.Factory.StartNew(() => { Dispatcher.BeginInvoke((Action) (async () => { try { var request = (HttpWebRequest) WebRequest.Create("http://www.google.com/s2/favicons?domain=" + _url); var response = await request.GetResponseAsync(); var responseStream = response.GetResponseStream(); var bmp = new Bitmap(responseStream); var brush = new SolidColorBrush(StaticFunctions.ToMediaColor(bmp.GetPixel(11, 11))); Grid.Background = brush; image.Source = BitmapToImageSource(bmp); var foreColor = PerceivedBrightness(StaticFunctions.ToMediaColor(bmp.GetPixel(11, 11))) > 130 ? Brushes.Black : Brushes.White; label.Foreground = foreColor; } catch { } })); }); }
static void Clamp(Bitmap bmp) { int R, G, B; Color value; int min = 75; int max = 180; for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { R = bmp.GetPixel(i, j).R; G = bmp.GetPixel(i, j).G; B = bmp.GetPixel(i, j).B; if (R > max) R = max; if (R < min) R = min; if (G > max) G = max; if (G < min) G = max; if (B > max) B = max; if (B < min) B = max; value = Color.FromArgb(R, G, B); bmp.SetPixel(i, j, value); } } Print(bmp, "clamp"); }
public static Bitmap encode(Bitmap img, int offSet) { Bitmap newImage = new Bitmap(img.Width * offSet, img.Height * offSet); List<List<Color>> pixelMap = new List<List<Color>>(); for (int y = 0; y < img.Height; y++) { List<Color> row = new List<Color>(); for (int x = 0; x < img.Width; x++) { row.Add(img.GetPixel(x, y)); } pixelMap.Add(row); } Random r = new Random(); for (int y = 0; y < newImage.Height; y++) { for (int x = 0; x < newImage.Width; x++) { if (x % offSet == 0 && y % offSet == 0) { newImage.SetPixel(x, y, pixelMap[y / offSet][x / offSet]); } else { newImage.SetPixel(x, y, img.GetPixel(r.Next(0, img.Width - 1), r.Next(0, img.Height - 1))); } } } return newImage; }
public static Bitmap UniformQuantization(Bitmap bmp, int kR, int kG, int kB) { int[] red, green, blue; int r, g, b; Bitmap res = new Bitmap(bmp.Width, bmp.Height); //podziel red red = DividePalette(kR); //podziel green green = DividePalette(kG); //podziel blue blue = DividePalette(kB); for (int i = 0; i < bmp.Width; ++i) for (int j = 0; j < bmp.Height; ++j) { r = FindClosest(red, bmp.GetPixel(i, j).R); g = FindClosest(green, bmp.GetPixel(i, j).G); b = FindClosest(blue, bmp.GetPixel(i, j).B); res.SetPixel(i, j, Color.FromArgb(r, g, b)); } return res; }
/// <summary> /// 调整 RGB 色调 /// </summary> /// <param name="bitmap"></param> /// <param name="thresholdRed">红色阀值</param> /// <param name="thresholdBlue">蓝色阀值</param> /// <param name="thresholdGreen">绿色阀值</param> /// <returns></returns> public System.Drawing.Bitmap AdjustToCustomColor(System.Drawing.Bitmap bitmap, int thresholdRed, int thresholdGreen, int thresholdBlue) { for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { // 取得每一個 pixel var pixel = bitmap.GetPixel(x, y); //判斷是否超過255 如果超過就是255 var pG = pixel.G + thresholdGreen; //如果小於0就為0 if (pG > 255) { pG = 255; } if (pG < 0) { pG = 0; } //判斷是否超過255 如果超過就是255 var pR = pixel.R + thresholdRed; //如果小於0就為0 if (pR > 255) { pR = 255; } if (pR < 0) { pR = 0; } //判斷是否超過255 如果超過就是255 var pB = pixel.B + thresholdBlue; //如果小於0就為0 if (pB > 255) { pB = 255; } if (pB < 0) { pB = 0; } // 將改過的 RGB 寫回 // 只寫入綠色的值 , R B 都放零 System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, pR, pG, pB); bitmap.SetPixel(x, y, newColor); } } return(bitmap); }
public void Process(string path) { Console.Clear(); ConsoleColor[] col = { ConsoleColor.White, ConsoleColor.Black, ConsoleColor.Blue, ConsoleColor.Cyan, ConsoleColor.DarkBlue, ConsoleColor.DarkCyan, ConsoleColor.DarkGray, ConsoleColor.DarkGreen, ConsoleColor.DarkMagenta, ConsoleColor.DarkRed, ConsoleColor.DarkYellow, ConsoleColor.Gray, ConsoleColor.Green, ConsoleColor.Magenta, ConsoleColor.Red, ConsoleColor.Yellow }; int[] r = { 255, 0, 0, 0, 0, 0, 169, 0, 139, 139, 204, 211, 0, 255, 255, 255 }; int[] g = { 255, 0, 0, 255, 0, 139, 169, 100, 0, 0, 204, 211, 255, 0, 0, 255 }; int[] b = { 255, 0, 255, 255, 139, 139, 169, 0, 139, 0, 0, 211, 0, 255, 0, 0 }; System.Drawing.Bitmap image = new System.Drawing.Bitmap(path); Color d; double mindev = Int32.MaxValue, dev = Int32.MaxValue; int minind = 0; for (int i = 0; i < image.Width; i++) { for (int j = 0; j < image.Height; j++) { d = image.GetPixel(i, j); mindev = Int32.MaxValue; for (int k = 0; k < 16; k++) { dev = Math.Pow(((d.R - r[k]) * 0.3), 2.0) + Math.Pow(((d.G - g[k]) * 0.59), 2.0) + Math.Pow(((d.B - b[k]) * 0.11), 2.0); if (mindev > dev) { mindev = dev; minind = k; } } Console.SetCursorPosition(i, j); Console.BackgroundColor = col[minind]; Console.Write(" "); } } Console.ReadKey(); }
private void Light_reduction_Click(object sender, EventArgs e) { try { if (curBitmap != null) { Color pixel; int red, green, blue; for (int x = 0; x < curBitmap.Width; x++) { for (int y = 0; y < curBitmap.Height; y++) { pixel = curBitmap.GetPixel(x, y); red = (int)(pixel.R * 0.6); green = (int)(pixel.G * 0.6); blue = (int)(pixel.B * 0.6); curBitmap.SetPixel(x, y, Color.FromArgb(red, green, blue)); } } pB.Image = curBitmap.Clone() as Image; } } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示"); } }
public static void doSomethingWithBitmapSlow(System.Drawing.Bitmap bmp) { for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { Color clr = bmp.GetPixel(x, y); int red = clr.R; int green = clr.G; int blue = clr.B; Console.WriteLine("Slow: " + red + " " + green + " " + blue); } } }
private void button3_Click(object sender, EventArgs e) { System.Drawing.Bitmap img = new System.Drawing.Bitmap(textBox1.Text); string mess = ""; Color lastpixel = img.GetPixel(img.Width - 1, img.Height - 1); int messlenght = lastpixel.B; for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { Color pixel = img.GetPixel(i, j); if (i < 1 && j < messlenght) { int value = pixel.B; char c = Convert.ToChar(value); string letter = System.Text.Encoding.ASCII.GetString(new byte[] { Convert.ToByte(c) }); mess = mess + letter; } } } textBox2.Text = mess; }
// take sensor values from Image public void TakeSnapshot(System.Drawing.Bitmap bmp) { //Take pixels for (int x = 0; x < _xdim; x++) { for (int y = 0; y < _ydim; y++) { int xPos = (int)((double)(bmp.Width) / (double)(_xdim) * x); int yPos = (int)((double)(bmp.Height) / (double)(_ydim) * y); Color color = bmp.GetPixel(xPos, yPos); int colorArgb = color.ToArgb(); _values[x, y] = colorArgb; } } }
private void btn保存非调色板图像_Click(object sender, EventArgs e) { if (pictureBox1.Image == null) { return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "tif files (*.tif)|*.tif|png files|*.png"; if (!sfd.ShowDialog().Equals(DialogResult.OK)) { return; } string ext = System.IO.Path.GetExtension(sfd.FileName); System.Drawing.Bitmap gb = (System.Drawing.Bitmap)pictureBox1.Image; if (gb.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) { System.Drawing.Bitmap newb = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { newb.SetPixel(x, y, gb.GetPixel(x, y)); } } switch (ext) { case ".png": { newb.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Png); break; } case ".tif": { newb.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Tiff); break; } default: { newb.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Tiff); break; } } } }
private RGBColor getColor(bool wrapped, int u, int v) { RGBColor texel; int RGB; if (!tile() && wrapped) { return(diffuseColor()); } RGB = texture.GetPixel(u, v).ToArgb(); texel = new RGBColor((RGB >> 16) & 0xff, (RGB >> 8) & 0xff, RGB & 0xff); return(texel.convertFrom24Bits()); }
public static SFML.Graphics.Image ToSFMLImage(System.Drawing.Bitmap bmp) { SFML.Graphics.Color[,] sfmlcolorarray = new SFML.Graphics.Color[bmp.Height, bmp.Width]; SFML.Graphics.Image newimage = null; for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { System.Drawing.Color csharpcolor = bmp.GetPixel(x, y); sfmlcolorarray[y, x] = new SFML.Graphics.Color(csharpcolor.R, csharpcolor.G, csharpcolor.B, csharpcolor.A); } } newimage = new SFML.Graphics.Image(sfmlcolorarray); return(newimage); }
public static Bitmap ImageFilter(Bitmap Image, int Size) { System.Drawing.Bitmap TempBitmap = Image; System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height); System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap); NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel); NewGraphics.Dispose(); Random TempRandom = new Random(); int ApetureMin = -(Size / 2); int ApetureMax = (Size / 2); for (int x = 0; x < NewBitmap.Width; ++x) { for (int y = 0; y < NewBitmap.Height; ++y) { List <int> RValues = new List <int>(); List <int> GValues = new List <int>(); List <int> BValues = new List <int>(); for (int x2 = ApetureMin; x2 < ApetureMax; ++x2) { int TempX = x + x2; if (TempX >= 0 && TempX < NewBitmap.Width) { for (int y2 = ApetureMin; y2 < ApetureMax; ++y2) { int TempY = y + y2; if (TempY >= 0 && TempY < NewBitmap.Height) { Color TempColor = TempBitmap.GetPixel(TempX, TempY); RValues.Add(TempColor.R); GValues.Add(TempColor.G); BValues.Add(TempColor.B); } } } } RValues.Sort(); GValues.Sort(); BValues.Sort(); Color MedianPixel = Color.FromArgb(RValues[RValues.Count / 2], GValues[GValues.Count / 2], BValues[BValues.Count / 2]); NewBitmap.SetPixel(x, y, MedianPixel); } } return(NewBitmap); }
/*/// <summary> * /// This method converts the read in byte arry to a System.Drawing.Bitmap * /// </summary> * /// <param name="data"> the byte array to be convereted</param> * /// <returns></returns> * public System.Drawing.Bitmap byteArrayToBitMap(byte[] data){ * System.Drawing.Bitmap bmp; * * System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter(); * bmp = (System.Drawing.Bitmap)ic.ConvertFrom(data); * //Debug.Log("Converted byteArray to bit map"); * return bmp; * * }*/ /* * public void byteArrayToTexture2D(byte[] data) * { * int width = data[6]; * int height = data[8]; * Debug.Log("Width: " + width + " Height: " + height); * Texture2D bmp = new Texture2D(width, height); * bmp.LoadRawTextureData(data); * myRawImage.texture = bmp; * }*/ /// <summary> /// This handles loading all fo the data from the given url and converts it into a readable image type and then allows the OnGUI function to draw the gif /// </summary> public void loadImage() { //Debug.Log("Called Load Image BACK"); gifImage = ByteArrayToImage(www.bytes); if (gifImage == null) { return; } isGif = true; var dimension = new System.Drawing.Imaging.FrameDimension(gifImage.FrameDimensionsList[0]); int frameCount = gifImage.GetFrameCount(dimension); Debug.Log("Dimensions: Frames: " + frameCount + " Width: " + gifImage.Width + " Height: " + gifImage.Height + " Pixels: " + (gifImage.Width * gifImage.Height)); int width = 0; int height = 0; for (int i = 0; i < frameCount; i++) { gifImage.SelectActiveFrame(dimension, i); var frame = new System.Drawing.Bitmap(gifImage.Width, gifImage.Height); System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty); Texture2D frameTexture = new Texture2D(frame.Width, frame.Height); for (int x = 0; x < frame.Width; x++) { for (int y = 0; y < frame.Height; y++) { System.Drawing.Color sourceColor = frame.GetPixel(x, y); frameTexture.SetPixel(frame.Width + x + 1, -y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped } } frameTexture.Apply(); gifFrames.Add(frameTexture); width = frame.Width; height = frame.Height; } byteArrayTextConversion(bytearrayholder); //Debug.Log("Starting ON GUI!"); canOnGUI = true; }
private void captureTimer_Tick(object sender, EventArgs e) { var width = 1; var height = 1; var image = new System.Drawing.Bitmap(width, height); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image)) { g.CopyFromScreen(position.X, position.Y, 0, 0, new System.Drawing.Size(width, height)); int sum = 0; for (int x = 0; x < image.Width; x++) { for (int y = 0; y < image.Height; y++) { var pixelColor = image.GetPixel(x, y); sum += pixelColor.R + pixelColor.G + pixelColor.B; } } avgRGB = sum / 3; if (!didSetCursor) { Position = System.Windows.Forms.Control.MousePosition; } Console.WriteLine(avgRGB); if (avgRGB >= 210) { if (!didSetCursor) { didSetCursor = true; SettingDAO.updateValueByKey("DEFAULTX", Position.X.ToString()); SettingDAO.updateValueByKey("DEFAULTY", Position.Y.ToString()); captureTimer.Stop(); System.Windows.MessageBoxResult result = System.Windows.MessageBox.Show("进战图标位置重设完成,若是不小心移动到其他白色区域而触发此对话框,可以再" + "点击刚才的按钮继续重新设定,请确保最终位置是在进入战斗图标(两柄交叉小剑)的交叉处或者刀刃处(最白的地方)", "重设确认", System.Windows.MessageBoxButton.OK); } else { Console.WriteLine("Battel Start"); dl.BattelDidStart(); isBattelStarted = true; captureTimer.Stop(); } } } }
private static System.Windows.Media.Color GetDominantColor(System.Drawing.Bitmap bitmap) { var newBitmap = new System.Drawing.Bitmap(1, 1); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newBitmap)) { // Interpolation mode needs to be HighQualityBilinear or HighQualityBicubic // or this method doesn't work. With either setting, the averaging result is // slightly different. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(bitmap, new System.Drawing.Rectangle(0, 0, 1, 1)); } System.Drawing.Color color = newBitmap.GetPixel(0, 0); return(System.Windows.Media.Color.FromRgb(color.R, color.G, color.B));; }
private System.Drawing.Bitmap NegativeConvert(System.Drawing.Image img) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(img); for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { var pixel = bitmap.GetPixel(x, y); System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, 255 - pixel.R, 255 - pixel.G, 255 - pixel.B); bitmap.SetPixel(x, y, newColor); } } return(bitmap); }
protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e) { if (CustomCheck == null) { CustomCheck = new System.Drawing.Bitmap(e.Image); for (int y = 0; (y <= (CustomCheck.Height - 1)); y++) { for (int x = 0; (x <= (CustomCheck.Width - 1)); x++) { var c = CustomCheck.GetPixel(x, y); CustomCheck.SetPixel(x, y, System.Drawing.Color.FromArgb(c.A, 255 - c.R, 255 - c.G, 255 - c.B)); } } } e.Graphics.DrawImage(CustomCheck, e.ImageRectangle); }
/// <summary> /// Convertir a escala de grises, es necesario optimizarlo con LockBitmap /// </summary> /// <param name="Bitmap"></param> /// <returns></returns> public static Bitmap GrayScale(System.Drawing.Bitmap Bitmap) { System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Bitmap.Clone(); System.Drawing.Color color; for (System.Int32 i = 0; i < bitmap.Width; i++) { for (System.Int32 j = 0; j < bitmap.Height; j++) { color = bitmap.GetPixel(i, j); color = ColorToGrey(color); Bitmap.SetPixel(i, j, color); } } return(Bitmap); }
public void OnCreate(object hook) { m_pApplication = hook as IApplication; m_pMxDoc = (IMxDocument)m_pApplication.Document; m_pMap = (IMap)m_pMxDoc.FocusMap; UID pUID = new UIDClass(); pUID.Value = "CoM_GISTools.CExtension"; m_pExtension = (IExtensionConfig)m_pApplication.FindExtensionByCLSID(pUID); m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream("CoM_GISTools.Images.print_orange.bmp")); // CoM_GISTools.DataFrame.CDataFrame.bmp")); if (m_bitmap != null) { m_bitmap.MakeTransparent(m_bitmap.GetPixel(1, 1)); m_hBitmap = m_bitmap.GetHbitmap(); } }
private static byte[] CopySafe(Bitmap bitmap) { int w = bitmap.Width, h = bitmap.Height; var r = new byte[w * h * 4]; for (int i = 0, y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var c = bitmap.GetPixel(x, y); r[i++] = c.B; r[i++] = c.G; r[i++] = c.R; r[i++] = c.A; } } return(r); }
private void btn在3D窗口里使用_Click(object sender, EventArgs e) { System.Drawing.Bitmap gb = (System.Drawing.Bitmap)pictureBox1.Image; System.Drawing.Bitmap newb = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { newb.SetPixel(x, y, gb.GetPixel(x, y)); } } byte[] TexDataIn = new byte[w * h * 4]; System.Drawing.Imaging.BitmapData bdata = newb.LockBits(Rectangle.FromLTRB(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(bdata.Scan0, TexDataIn, 0, w * h * 4); newb.UnlockBits(bdata); InputTex(TexDataIn, w, h, w * h * 4); }
//RGB resim için renk aralığı dizisi oluşturur. public long[] GetHistogram(System.Drawing.Bitmap picture) { long[] myHistogram = new long[256]; for (int i = 0; i < picture.Size.Height; i++) { for (int j = 0; j < picture.Size.Width; j++) { Color c = picture.GetPixel(j, i); myHistogram[c.R]++; myHistogram[c.G]++; myHistogram[c.B]++; } } return(myHistogram); }
public System.Drawing.Bitmap KnockOutGzf(String path) { System.Drawing.Image image = System.Drawing.Image.FromFile(path); System.Drawing.Bitmap bitmapProxy = new System.Drawing.Bitmap(image); image.Dispose(); for (int i = 0; i < bitmapProxy.Width; i++) { for (int j = 0; j < bitmapProxy.Height; j++) { System.Drawing.Color c = bitmapProxy.GetPixel(i, j); if (!(c.R < 240 || c.G < 240 || c.B < 240)) { bitmapProxy.SetPixel(i, j, System.Drawing.Color.Transparent); } } } return(bitmapProxy); }
protected Int64 generateHashFor(System.Drawing.Image image, int imageColorToSearchFor) { Int64 two = 2; // BigInteger.valueOf(2); Int64 hash = 1; // BigInteger.ONE; var bmp = new System.Drawing.Bitmap(image); for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { var rgb = bmp.GetPixel(x, y).ToArgb(); hash = hash * two + (rgb == imageColorToSearchFor ? 0 : 1); //hash = hash.multiply(two).add(BigInteger.valueOf((image.getRGB(x, y) == imageColorToSearchFor ? 0 : 1))); } } return(hash); }
/// <summary> /// Handles the Tick event of the timer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> void timer_Tick(object sender, EventArgs e) { var graphics = System.Drawing.Graphics.FromImage(printscreen as Image); // Get the image of the captured screen graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); // Get the copy of screen var position = Cursor.Position; // Get the position of cursor var lensbmp = new System.Drawing.Bitmap(50, 50); // Have a bitmap for lens var i = 0; // Variable for row count var j = 0; // Variable for column count for (int row = position.X - 25; row < position.X + 25; row++) // Indicates row number { j = 0; // Set column value '0' for new column for (int column = position.Y - 25; column < position.Y + 25; column++) // Indicate column number { lensbmp.SetPixel(i, j, printscreen.GetPixel(row, column)); // Place current region pixel to lens bitmap j++; // Increase row count } i++; // Increase column count } this.magnifierScreen.Image = new System.Drawing.Bitmap(lensbmp, lensbmp.Width * magnifierZoom, lensbmp.Height * magnifierZoom); // Assign lens bitmap with magnifierZoom level to the picture box Size = magnifierScreen.Image.Size; // Assign optimal value to the form Left = position.X + 20; // Place form nearer to cursor X value Top = position.Y + 20; // Place form nearer to cursor Y value TopMost = true; // Keep the form top level lensbmp.Dispose(); graphics.Dispose(); }
/// <summary> /// 指定したリソースを System.Drawing.Bitmap として取得します。 /// </summary> /// <param name="asm">リソース読み取り元のアセンブリ識別文字列を指定します。</param> /// <param name="key">リソースの名前を指定します。</param> /// <param name="transparent">左上の色を投下させるかどうかを指定します。</param> /// <returns>取得した Bitmap を返します。</returns> private static System.Drawing.Bitmap GetBitmap(string asm, string key, bool transparent) { if (bmps.ContainsKey(key)) { return(bmps[key]); } System.Drawing.Bitmap bmp = ReadBitmap(asms[asm], key); if (transparent) { afh.Drawing.BitmapEffect.ReplaceColor( bmp, (afh.Drawing.Color32Argb)bmp.GetPixel(0, 0), (afh.Drawing.Color32Argb)System.Drawing.Color.Transparent ); } return(bmps[key] = bmp); }
/// <summary> /// Image Pixelate /// </summary> /// <param name="image">Set Bitmap</param> /// <param name="rectangle">Set rectangle</param> /// <param name="pixelateSize">Size of pixelation</param> /// <returns></returns> public static System.Drawing.Bitmap Pixelate(System.Drawing.Bitmap image, Rectangle rectangle, Int32 pixelateSize) { System.Drawing.Bitmap pixelated = new System.Drawing.Bitmap(image.Width, image.Height); // make an exact copy of the bitmap provided using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(pixelated)) graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); // look at every pixel in the rectangle while making sure we're within the image bounds for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize) { for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize) { Int32 offsetX = pixelateSize / 2; Int32 offsetY = pixelateSize / 2; // make sure that the offset is within the boundry of the image while (xx + offsetX >= image.Width) { offsetX--; } while (yy + offsetY >= image.Height) { offsetY--; } // get the pixel color in the center of the soon to be pixelated area Color pixel = pixelated.GetPixel(xx + offsetX, yy + offsetY); // for each pixel in the pixelate size, set it to the center color for (Int32 x = xx; x < xx + pixelateSize && x < image.Width; x++) { for (Int32 y = yy; y < yy + pixelateSize && y < image.Height; y++) { pixelated.SetPixel(x, y, pixel); } } } } return(pixelated); }
private bool PaintBox(Graphics g, Pen pen, Rectangle r) { bool vote = false; int red = 0, green = 0, blue = 0, alpha = 0; for (int i = 0; i < r.Width; i++) { for (int j = 0; j < r.Height; j++) { var pixel = _bitmap.GetPixel(r.X + i, r.Y + j); red += pixel.R; green += pixel.G; blue += pixel.B; alpha += pixel.A; } } Func <int, int> avg = c => c / (r.Width * r.Height); red = avg(red); green = avg(green); blue = avg(blue); alpha = avg(alpha); var color = Color.FromArgb(alpha, red, green, blue); //System.Diagnostics.Debug.WriteLine("{0} {1} {2} {3}", alpha, red, green, blue); if ((red + green + blue) / 3 > 50) { vote = true; color = Color.White; } g.DrawRectangle(pen, r); System.Drawing.Brush b = new System.Drawing.SolidBrush(color); g.FillRectangle(b, Rectangle.FromLTRB(r.Left + 50, r.Top, r.Right + 50, r.Bottom)); b.Dispose(); return(vote); }
private static System.Drawing.Color GenerateOnePixel(System.Drawing.Bitmap src, int PosW, int PosH) { int Ob = 0; int Og = 0; int Or = 0; int Oa = 0; int SumKernel = 0; for (int h = 0; h < 2; h++) { int NowH = PosH * 2 + h; if (NowH > src.Height - 1) { continue; } for (int w = 0; w < 2; w++) { int NowW = PosW * 2 + w; if (NowW > src.Width - 1) { continue; } var p = src.GetPixel(NowW, NowH); Oa += (int)p.A * Kernel[h, w]; Ob += (int)p.B * Kernel[h, w]; Og += (int)p.G * Kernel[h, w]; Or += (int)p.R * Kernel[h, w]; SumKernel += Kernel[h, w]; } } if (SumKernel == 0) { return(System.Drawing.Color.FromArgb(0)); } Oa /= SumKernel; Ob /= SumKernel; Og /= SumKernel; Or /= SumKernel; return(System.Drawing.Color.FromArgb(Oa, Or, Og, Ob)); }
// calibrate the rgb private void CalibrateRGB(float exposureTime) { StringBuilder matrixBuildStr = new StringBuilder(); string fullname = string.Format("{0}{1}_RGB.txt", PATH, this.serialNumber); List <double[, , ]> rgbValue = new List <double[, , ]>(); System.Drawing.Bitmap bitmap = null; System.Drawing.Color pixel; foreach (int[] item in this.rgbList) { matrixBuildStr.AppendFormat("[Set Panel's RGB = ({0},{1},{2})]\r\n", item[0], item[1], item[2]); if (dut.ChangePanelColor(item[0], item[1], item[2])) { System.Threading.Thread.Sleep(5000); camera.ExposureTime = this.CalExposureTime(exposureTime, item); bitmap = camera.GrabImage(); int w = Convert.ToInt32(System.Math.Round(bitmap.Width * 0.5)); int h = Convert.ToInt32(System.Math.Round(bitmap.Height * 0.5)); for (int i = h - 5; i < h + 5; i++) { for (int j = w - 5; j < w + 5; j++) { pixel = bitmap.GetPixel(i, j); matrixBuildStr.AppendFormat("({0},{1},{2})", pixel.R, pixel.G, pixel.B); } matrixBuildStr.AppendLine(); } } matrixBuildStr.AppendLine(); } using (StreamWriter sw = new StreamWriter(fullname, true)) { sw.Write(matrixBuildStr.ToString()); sw.Flush(); sw.Close(); } }
/// <summary> /// 红色滤镜 /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="threshold">阀值 -255~255</param> /// <returns></returns> public System.Drawing.Bitmap AdjustToRed(System.Drawing.Bitmap bitmap, int threshold) { for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { // 取得每一個 pixel var pixel = bitmap.GetPixel(x, y); var pR = pixel.R + threshold; pR = Math.Max(pR, 0); pR = Math.Min(255, pR); // 將改過的 RGB 寫回 // 只寫入紅色的值 , G B 都放零 System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, pR, 0, 0); bitmap.SetPixel(x, y, newColor); } } // 回傳結果 return(bitmap); }
public static bool ConvertToGray(System.Drawing.Bitmap b) { for (int i = 0; i < b.Width; i++) { for (int j = 0; j < b.Height; j++) { Color c1 = b.GetPixel(i, j); int r1 = c1.R; int g1 = c1.G; int b1 = c1.B; int gray = (byte)(.299 * r1 + .587 * g1 + .114 * b1); r1 = gray; g1 = gray; b1 = gray; b.SetPixel(i, j, Color.FromArgb(r1, g1, b1)); } } return(true); }