private static void SetPixel(int x, int y, System.Drawing.Color color, System.Drawing.Bitmap bmp) { lock (bmpLock) { bmp.SetPixel(x, y, color); } }
/// <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; }
/// <summary> /// 绿色滤镜 /// </summary> /// <param name="bitmap">一个图片实例</param> /// <param name="threshold">阀值 -255~+255</param> /// <returns></returns> public System.Drawing.Bitmap AdjustToGreen(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); //判斷是否超過255 如果超過就是255 var pG = pixel.G + threshold; //如果小於0就為0 if (pG > 255) pG = 255; if (pG < 0) pG = 0; // 將改過的 RGB 寫回 // 只寫入綠色的值 , R B 都放零 System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, 0, pG, 0); bitmap.SetPixel(x, y, newColor); } } // 回傳結果 return bitmap; }
/// <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 ExportHeightsToBitmap( System.Drawing.Bitmap bitmap, float MaxHeight, float MinHeight ) { float mult = 5.0f / 3.0f; float range = ( MaxHeight - MinHeight ); for ( int i = 0; i < this._vertices.Length; i++ ) { int x = int.MaxValue, y = int.MaxValue, z = int.MaxValue; float zf=float.MaxValue; Tools.Vector<float> position = this._vertices[i].Position; try { x = int.Parse( Math.Round( position[0] / mult ).ToString() ); y = int.Parse( Math.Round( position[1] / mult ).ToString() ); zf = 255.0f * ( position[2] - MinHeight ) / range; z = !float.IsNaN( zf ) ? int.Parse( Math.Round( zf ).ToString() ) : 124; System.Drawing.Color col = System.Drawing.Color.FromArgb( z, z, z ); bitmap.SetPixel( x, y, col ); } catch ( System.Exception ex ) { System.Windows.Forms.MessageBox.Show( "Crapped Out:\n" + ex.Message + "\n" + ex.StackTrace ); System.Windows.Forms.MessageBox.Show( string.Format("Position: {0} x={1} y={2} z={3} zf={4}", position, x, y, z, zf ) ); throw ex; } } }
public void ExportColoursToBitmap( System.Drawing.Bitmap bitmap ) { double mult = Constants.VertexSpacing; for ( int i = 0; i < this._vertices.Length; i++ ) { int x = int.MaxValue, y = int.MaxValue; Tools.Vector<float> position = this._vertices[i].Position; try { x = int.Parse( Math.Round( position[0] / mult ).ToString() ); y = int.Parse( Math.Round( position[1] / mult ).ToString() ); System.Drawing.Color col = this._vertices[i].Color; bitmap.SetPixel( x, y, col ); } catch ( System.Exception ex ) { System.Windows.Forms.MessageBox.Show( "Crapped Out Ixporting Colours:\n" + ex.Message + "\n" + ex.StackTrace ); throw ex; } } }
private static void WritePixelsToBitmap( Palette palette, IList<byte> pixels, System.Drawing.Bitmap destination ) { int width = destination.Width; int height = destination.Height; for ( int i = 0; i < pixels.Count; i++ ) { destination.SetPixel( i % width, i / width, palette.Colors[pixels[i]] ); } }
public bool WriteBitmapTo(Stream dataStream, bool bits8, ref System.Drawing.Bitmap img, System.Drawing.Point point) { BinaryReader rdr = new BinaryReader(dataStream); byte[] data = rdr.ReadBytes((int)Length); int index = 0; int x = point.X; int y = point.Y; if (bits8 == true) { switch (type) { case Types.Floor: int i = 1; int c = 23; for (int row = 0; index < data.Length && y + row < img.Height; row++) { for (int col = 0; index < data.Length && col < i && col + c + x < img.Width; col++) img.SetPixel(col + c + x, row + y, System.Drawing.Color.FromArgb((int)(VideoBag.Palette[data[index++]]))); if (row < 22) { i += 2; c--; } else if (row > 22) { i -= 2; c++; } } img.Save("c:\\bit.bmp"); return true; } } return false; }
public static void SetPixelArea( System.Drawing.Bitmap bmp, int x, int y, System.Drawing.Color color, System.Drawing.Color border ) { int xext = 15, yext = 15; int bordersize = 3; for ( int i = x - xext; i < x + xext; ++i ) { bool isBorderX = i < x - xext + bordersize || i > x + xext - 1 - bordersize; for ( int j = y - yext; j < y + yext; ++j ) { bool isBorderY = j < y - yext + bordersize || j > y + yext - 1 - bordersize; if ( i >= 0 && i < bmp.Width && j >= 0 && j < bmp.Height ) { bmp.SetPixel( i, j, isBorderX || isBorderY ? border : color ); } } } }
private void DrawGlyphOnBitmap( System.Drawing.Bitmap b, Glyph g, System.Drawing.Point loc ) { for ( int i = 0; i < g.Pixels.Length; i++ ) { b.SetPixel( loc.X + i % 10, loc.Y + i / 10, colors[(int)g.Pixels[i]] ); } }
/// <summary> /// Writes raw OpenCV image data into the System.Drawing.Bitmap object. /// </summary> /// <param name="bitmap">This System.Drawing.Bitmap object needs to have the same width and height as the corresponding parameters.</param> /// <param name="imageData">This is a basically an C unsigned char pointer to the OpenCV image data.</param> /// <param name="width">The image's width</param> /// <param name="height">The image's height</param> /// <param name="channels">The image's number of channels</param> /// <param name="widthStep">The image's width step</param> unsafe private void convertToBitmap2(ref System.Drawing.Bitmap bitmap, ref byte* imageData, int width, int height, int channels, int widthStep) { unsafe { if (imageData == null) { return; } int nl = height; int nc = width * channels; int step = widthStep; int t = 0; for (int i = 0; i < nl; i++) { for (int j = 0; j < nc; j += channels) { //bitmap.SetPixel(j / 3, i, System.Drawing.Color.FromArgb(imageData[j + t], imageData[j + 1 + t], imageData[j + 2 + t])); // Somehow Red and Blue where switched. bitmap.SetPixel(j / 3, i, System.Drawing.Color.FromArgb(imageData[j + 2 + t], imageData[j + 1 + t], imageData[j + t])); } t += step; } } }
public void Cal(System.Drawing.Bitmap colorBitmap, ushort[] depthPixels, CameraSpacePoint[] ColorInSkel, ushort[] depth4background, int[] boolpixel) { int ColorCenterXsum = 0, ColorCenterYsum = 0, uuSum = 0, vvSum = 0, Cnt = 0; List<double> Xsum = new List<double>(); List<double> Ysum = new List<double>(); List<double> Zsum = new List<double>(); for (int i = (int)ColorCenter.X - SearchRange; i < ColorCenter.X + SearchRange; i++) { for (int j = (int)ColorCenter.Y - SearchRange; j < ColorCenter.Y + SearchRange; j++) { if (i <= 0 || i >= 511 || j <= 0 || j >= 423) break; //avoid edge prob. int di = (i + j * 512); //di = depthpixel index //int ci = di * 4; //ci = colorPixel index //nt ci = (int)(4 * ((i * 424 * 2.547) + (j * 512 * 3.75))); //ci = colorPixel index UU = -0.169 * colorBitmap.GetPixel(i, j).R - 0.331 * colorBitmap.GetPixel(i, j).G + 0.5 * colorBitmap.GetPixel(i, j).B + 128; VV = 0.5 * colorBitmap.GetPixel(i, j).R - 0.419 * colorBitmap.GetPixel(i, j).G - 0.081 * colorBitmap.GetPixel(i, j).B + 128; //UU = -0.169 * colorPixels[ci + 2] - 0.331 * colorPixels[ci + 1] + 0.5 * colorPixels[ci] + 128; //VV = 0.5 * colorPixels[ci + 2] - 0.419 * colorPixels[ci + 1] - 0.081 * colorPixels[ci] + 128; if (UU > AverageUU - uvRange && UU < AverageUU + uvRange && VV > AverageVV - uvRange && VV < AverageVV + uvRange && UU > CenterUU - CenterUVrange && UU < CenterUU + CenterUVrange && VV > CenterVV - CenterUVrange && VV < CenterVV + CenterUVrange && (boolpixel[di] == 0 || boolpixel[di] == TargetID)) //&& Math.Abs(depthPixels[di].Depth - depth4background[di].Depth) > DepthRange //深度背景相減 //&& (ColorInSkel[di].Z != 0 && ColorInSkel[di].Z < 2.5 )) //這裡先不管Z值部分,所以先不做 { boolpixel[di] = TargetID; uuSum += (int)UU; vvSum += (int)VV; colorBitmap.SetPixel(i, j, System.Drawing.Color.FromArgb(0, 0, 0, 0)); // (byte)depthPixels[depthIndex].Depth; ColorCenterXsum += i; ColorCenterYsum += j; Cnt++; //顏色追蹤歸顏色追蹤,Z值追蹤歸Z值追蹤 ;即使zCnt值不夠,UV值、中心點仍繼續更新 //int offsetindex = (i * 512 / 1920) + (j * 424 / 1080 * 512); if (ColorInSkel[di].Z != 0 && ColorInSkel[di].Z < TheDeepestDetectDistance) //2.5 // 必須要先轉,因為在後面做平均之後值會跑掉(雖然理論上不會阿QAQ) { //Xsum.Add(ColorInSkel[offsetindex].X); //Ysum.Add(ColorInSkel[offsetindex].Y); //Zsum.Add(ColorInSkel[offsetindex].Z); XYZ.X = ColorInSkel[di].X; XYZ.Y = ColorInSkel[di].Y; XYZ.Z = ColorInSkel[di].Z; Console.WriteLine(ColorInSkel[di].X.ToString() + "," + ColorInSkel[di].Y.ToString() + "," + ColorInSkel[di].Z.ToString()); AverageUU = UU; AverageVV = VV; ColorCenter.X = ColorCenterXsum / Cnt; ColorCenter.Y = ColorCenterYsum / Cnt; TrackState = true; //Cnt++; } else { SearchRange = 10; // TrackState = false; } } else boolpixel[di] = 0; } } //if (Cnt > 2) //{ // AverageUU = uuSum / Cnt; // AverageVV = vvSum / Cnt; // ColorCenter.X = ColorCenterXsum / Cnt; // ColorCenter.Y = ColorCenterYsum / Cnt; // if (Zsum.Count > 3) // { // XYZ.X = Xsum.Average(); // XYZ.Y = Ysum.Average(); // XYZ.Z = Zsum.Average(); // } // //height = XYZ.Z* Math.Sin((Math.PI / 180) * ((240 - ColorCenter.Y) / 240 * 27)) + KinectHeight; // 21.5=>27 // SearchRange = 5; // TrackState = true; //} //else //{ // TrackState = false; // SearchRange = 10; //} ColorCenterXsum = 0; ColorCenterYsum = 0; Cnt = 0; uuSum = 0; vvSum = 0; }
static object RenderRowChunk (System.Drawing.Bitmap canvas, int dotPeriod, int x, int y) { if (y >= CANVAS_HEIGHT) return null; if ((x == 0) && ((y % dotPeriod) == 0)) { System.Console.Write("*"); } int chunkSize = 32; JSIL.Verbatim.Expression("canvas.flushInterval = $0", chunkSize); stopwatch.Restart(); int x1 = x, x2 = Math.Min(x + chunkSize, CANVAS_WIDTH); for (; x < x2; x++) { Color c = RenderPixel(x, y); canvas.SetPixel(x, y, c); } var elapsed = stopwatch.ElapsedMilliseconds; double msPerPixel = (double)elapsed / chunkSize; if (x2 >= CANVAS_WIDTH) { y += 1; x2 = 0; } ReportSpeed(msPerPixel); bool useSetTimeout = JSIL.Builtins.IsJavascript; Func<object> next = () => RenderRowChunk(canvas, dotPeriod, x2, y); if (useSetTimeout) { SetTimeout(0, next); return null; } else return next; }
//метод инвертирование рисунка System.Drawing.Bitmap InvertMethod(System.Drawing.Bitmap bitmap) { int x; for (x = 0; x < bitmap.Width; x++) { for (int y = 0; y <bitmap.Height ; y++) { System.Drawing.Color oldColor = bitmap.GetPixel(x,y); System.Drawing.Color newColor; newColor = System.Drawing.Color.FromArgb(oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B); bitmap.SetPixel(x, y, newColor); } System.Threading.Thread.Sleep(5); bckGrWorker.ReportProgress((x*100)/bitmap.Width); } return bitmap; }
protected void ApplyPixelMatrix(System.Drawing.Bitmap bmp, int x, int y, byte curCol) { System.Drawing.Color black = System.Drawing.Color.FromArgb(255, 0, 0, 0); System.Drawing.Color white = System.Drawing.Color.FromArgb(255, 255, 255, 255); curCol = (byte)(curCol / ColorCount); if (curCol == 0) { for (int i = 0; i < DPP; ++i) { for (int j = 0; j < DPP; ++j) { bmp.SetPixel(x + i, y + j, black); } } } if (curCol != 0 && curCol != (DPP*DPP)) { int[,] mat = createRandomMatrix(DPP, DPP, curCol); for (int i = 0; i < DPP; ++i) { for (int j = 0; j < DPP; ++j) { if (mat[i, j] == 1) { bmp.SetPixel(x + i, y + j, white); } else { bmp.SetPixel(x + i, y + j, black); } } } } if (curCol == (DPP*DPP)) { for (int i = 0; i < DPP; ++i) { for (int j = 0; j < DPP; ++j) { bmp.SetPixel(x + i, y + j, white); } } } }
/// <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; }
private System.Drawing.Bitmap DrawCharacter(System.Drawing.Bitmap bmp, System.Drawing.Color color, int size) { for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { System.Drawing.Color pixel = bmp.GetPixel(x, y); if (pixel != System.Drawing.Color.FromArgb(0, 0, 0, 0)) { if (pixel == System.Drawing.Color.FromArgb(255, 249, 249, 249)) bmp.SetPixel(x, y, System.Drawing.Color.White); else { int red = (pixel.R + color.R) / 2; int green = (pixel.G + color.G) / 2; int blue = (pixel.B + color.B) / 2; bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(red, green, blue)); } } } } System.Drawing.Bitmap newImage = new System.Drawing.Bitmap(size, size); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newImage)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, size, size)); } return newImage; }
static void RenderRow (System.Drawing.Bitmap canvas, int dotPeriod, int y) { if (y >= CANVAS_HEIGHT) return; if ((y % dotPeriod) == 0) System.Console.Write("*"); stopwatch.Restart(); for (int x = 0; x < CANVAS_WIDTH; x++) { Color c = RenderPixel(x, y); canvas.SetPixel(x, y, c); } var elapsed = stopwatch.ElapsedMilliseconds; double msPerPixel = (double)elapsed / CANVAS_WIDTH; ReportSpeed(msPerPixel); SetTimeout(0, () => RenderRow(canvas, dotPeriod, y + 1) ); }
private static void CopyBitmapToBitmap( System.Drawing.Bitmap source, System.Drawing.Bitmap destination, System.Drawing.Point destPoint ) { int count = source.Width * source.Height; int width = source.Width; int destX = destPoint.X; int destY = destPoint.Y; for ( int i = 0; i < count; i++ ) { destination.SetPixel( destX + i % width, destY + i / width, source.GetPixel( i % width, i / width ) ); } }
public void finishLine(System.Drawing.Bitmap bmp_out) { //Console.WriteLine("({0}, {1}) - ({2}, {3})", outerFromPnt.x, outerFromPnt.y, toPnt.x, toPnt.y); if (outerFromPnt.x < 0 || outerFromPnt.y < 0 || outerFromPnt.x >= bmp_out.Width || outerFromPnt.y >= bmp_out.Height) {} else bmp_out.SetPixel(outerFromPnt.x, outerFromPnt.y, System.Drawing.Color.Green); if (toPnt.x < 0 || toPnt.y < 0 || toPnt.x >= bmp_out.Width || toPnt.y >= bmp_out.Height) {} else bmp_out.SetPixel(toPnt.x, toPnt.y, System.Drawing.Color.Green); }
static void RenderRow(System.Drawing.Bitmap canvas, int dotPeriod, int y) { if (y >= CANVAS_HEIGHT) return; if ((y % dotPeriod) == 0) System.Console.Write("*"); for (int x = 0; x < CANVAS_WIDTH; x++) { Color c = RenderPixel(x, y); canvas.SetPixel(x, y, c); } SetTimeout(0, () => RenderRow(canvas, dotPeriod, y + 1) ); }
public static void RenderSquare(ref System.Drawing.Bitmap image, int x, int y, int size, Color color, bool border) { Color myclr = color; for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { if (border && size >= 5) { if (i == 0 || j == 0) { myclr = Color.Black; //border } else { myclr = color; } } image.SetPixel(x + i, y + j, myclr); } } }