private void button1_Click(object sender, EventArgs e) { using (Bitmap bmp1 = new Bitmap(400, 500)) using (var bmplock = bmp1.Lock()) { WriteableBitmap wb = bmplock.GetWritableBitmap(); //lines int y = 0; wb.DrawLine(0, y, 100, y + 100, System.Windows.Media.Imaging.Color.FromArgb(255, 255, 0, 0)); //red wb.DrawLine(0, y + 100, 100, y + 0, System.Windows.Media.Imaging.Color.FromArgb(255, 0, 0, 255)); //blue wb.DrawLineAa(100, y, 200, y + 100, System.Windows.Media.Imaging.Color.FromArgb(255, 255, 0, 0)); wb.DrawLineAa(100, y + 100, 200, y + 0, System.Windows.Media.Imaging.Color.FromArgb(255, 0, 0, 255)); //blue //---------- y += 150; wb.DrawLineDDA(0, y, 100, y + 100, System.Windows.Media.Imaging.Color.FromArgb(255, 255, 0, 0)); //red wb.DrawLineDDA(0, y + 100, 100, y + 0, System.Windows.Media.Imaging.Color.FromArgb(255, 0, 0, 255)); //blue wb.DrawEllipse(200, 0, 300, 100, System.Windows.Media.Imaging.Color.FromArgb(255, 255, 0, 0)); // bmplock.WriteAndUnlock(); bmp1.Save("d:\\WImageTest\\a0002.png"); } }
/// <summary> /// Draws a colored line by connecting two points using a DDA algorithm (Digital Differential Analyzer). /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> public static void DrawLineDDA(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color) { // Add one to use mul and cheap bit shift for multiplicaltion int a = color.A + 1; int col = (color.A << 24) | ((byte)((color.R * a) >> 8) << 16) | ((byte)((color.G * a) >> 8) << 8) | ((byte)((color.B * a) >> 8)); bmp.DrawLineDDA(x1, y1, x2, y2, col); }
protected void DrawLine(WriteableBitmap bitmap, Line line, Color color) { bitmap.DrawLineDDA((int)(line.from.X * width), (int)(line.from.Y * height), (int)(line.to.X * width), (int)(line.to.Y * height), color); }
/// <summary> /// Draws a colored line by connecting two points using a DDA algorithm (Digital Differential Analyzer). /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> public static void DrawLineDDA(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color) { bmp.DrawLineDDA(x1, y1, x2, y2, (color.A << 24) | (color.R << 16) | (color.G << 8) | color.B); }
public void Draw(WriteableBitmap bitmap) { bitmap.Clear(Colors.White); double width = bitmap.Width; double halfWidth = width / 2; width -= 1; double height = bitmap.Height; double uParam = P.FirstParamLimit; double vParam = P.SecondParamLimit; double sParam = Q.FirstParamLimit; double tParam = Q.SecondParamLimit; using (bitmap.GetBitmapContext()) { if (QtestSet != null) { var uStep = Q.FirstParamLimit / (QtestSet.GetLength(1) - 1.0); var vStep = Q.SecondParamLimit / (QtestSet.GetLength(0) - 1.0); for (int i = 0; i < QtestSet.GetLength(1); i++) { for (int j = 0; j < QtestSet.GetLength(0); j++) { bitmap.DrawEllipseCentered((int)(i * uStep / sParam * (halfWidth - 3) + halfWidth), (int)(j * vStep / tParam * height), 1, 1, QtestSet[j, i] ? Colors.LightGray : Colors.LightBlue); } } } if (pPolygon != null) { var minSize = System.Math.Min(P.FirstParamLimit, P.SecondParamLimit) * 0.8; for (int i = 0; i < pPolygon.Count - 1 + (cyclic ? 1 : 0); i++) { var item1 = pPolygon[i]; var item2 = pPolygon[(i + 1) % pPolygon.Count]; if (System.Math.Abs(item1.X - item2.X) < minSize && System.Math.Abs(item1.Y - item2.Y) < minSize) { bitmap.DrawLineDDA((int)(item1.X / uParam * halfWidth), (int)(item1.Y / vParam * height), (int)(item2.X / uParam * halfWidth), (int)(item2.Y / vParam * height), Colors.BlueViolet); } } } if (IsIntersectableP) { if (pPolygonBoundary != null) { foreach (var tuple in pPolygonBoundary) { bitmap.DrawLineDDA((int)(tuple.Item1.X / uParam * halfWidth), (int)(tuple.Item1.Y / vParam * height), (int)(tuple.Item2.X / uParam * halfWidth), (int)(tuple.Item2.Y / vParam * height), Colors.BlueViolet); } } } if (qPolygon != null) { var minSize = System.Math.Min(Q.FirstParamLimit, Q.SecondParamLimit) * 0.8; for (int i = 0; i < qPolygon.Count - 1 + (cyclic ? 1 : 0); i++) { var item1 = qPolygon[i]; var item2 = qPolygon[(i + 1) % qPolygon.Count]; if (System.Math.Abs(item1.X - item2.X) < minSize && System.Math.Abs(item1.Y - item2.Y) < minSize) { bitmap.DrawLineDDA((int)(item1.X / sParam * (halfWidth - 3) + halfWidth), (int)(item1.Y / tParam * height), (int)(item2.X / sParam * (halfWidth - 3) + halfWidth), (int)(item2.Y / tParam * height), Colors.DarkViolet); } } } if (IsIntersectableQ) { if (qPolygonBoundary != null) { foreach (var tuple in qPolygonBoundary) { bitmap.DrawLineDDA((int)(tuple.Item1.X / sParam * (halfWidth - 3) + halfWidth), (int)(tuple.Item1.Y / tParam * height), (int)(tuple.Item2.X / sParam * (halfWidth - 3) + halfWidth), (int)(tuple.Item2.Y / tParam * height), Colors.DarkViolet); } } } //if (PtestSet != null) //{ // var uStep = P.FirstParamLimit / (PtestSet.GetLength(1) - 1.0); // var vStep = P.SecondParamLimit / (PtestSet.GetLength(0) - 1.0); // for (int i = 0; i < PtestSet.GetLength(1); i++) // for (int j = 0; j < PtestSet.GetLength(0); j++) // { // bitmap.DrawEllipseCentered((int)(i * uStep / uParam * halfWidth), (int)(j * vStep / vParam * height), 1, 1, PtestSet[j, i] ? Colors.Green : Colors.Red); // } //} } }
void canvas_PointerReleased(object sender, PointerRoutedEventArgs e) { if (e.Pointer.PointerId == PenID || e.Pointer.PointerId == TouchID) { MyInkManager.ProcessPointerUp(e.GetCurrentPoint(canvas)); } TouchID = 0; PenID = 0; e.Handled = true; Pencil = null; NewLine = null; NewRectangle = null; NewEllipse = null; //to draw line on our writable bitmap not on canvas. if (itisline) { wb.DrawLineDDA((int)lineX1, (int)lineY1, (int)lineX2, (int)lineY2, clr); } //to draw line on our actual rectangle. if (itisrectangle) { if (rectX2 > rectX1 && rectY2 > rectY1) { wb.DrawRectangle((int)rectX1, (int)rectY1, (int)rectX2, (int)rectY2, clr); } else if (rectX2 < rectX1 && rectY2 < rectY1) { wb.DrawRectangle((int)rectX2, (int)rectY2, (int)rectX1, (int)rectY1, clr); } else if (rectX2 > rectX1 && rectY2 < rectY1) { wb.DrawRectangle((int)rectX1, (int)rectY2, (int)rectX2, (int)rectY1, clr); } else if (rectX2 < rectX1 && rectY2 > rectY1) { wb.DrawRectangle((int)rectX2, (int)rectY1, (int)rectX1, (int)rectY2, clr); } } // to draw a ellipse if (itisellipse) { if (elipX2 > elipX1 && elipY2 > elipY1) { wb.DrawEllipse((int)elipX1, (int)elipY1, (int)elipX2, (int)elipY2, clr); } else if (elipX2 < elipX1 && elipY2 < elipY1) { wb.DrawEllipse((int)elipX2, (int)elipY2, (int)elipX1, (int)elipY1, clr); } else if (elipX2 > elipX1 && elipY2 < elipY1) { wb.DrawEllipse((int)elipX1, (int)elipY2, (int)elipX2, (int)elipY1, clr); } else if (elipX2 < elipX1 && elipY2 > elipY1) { wb.DrawEllipse((int)elipX2, (int)elipY1, (int)elipX1, (int)elipY2, clr); } } }