c# Winforms - Graphics/GDI pen color stuck as black -
update: code has been updated , seems working pretty well. feel free comment.
so have program takes screenshot , lets user draw on screen. similar snipping tool has different purpose irrelevant this.
i @ page: link here , interested in how works. able adapt program color work right. reason, draws in black!!!
here have:
public partial class drawtool : form { private bool is_mouse_down { get; set; } // check if mouse down or not. list<list<point>> list_points = new list<list<point>>(); private int count { get; set; } private bool start { get; set; } public drawtool() { initializecomponent(); picturebox1.dock = dockstyle.fill; picturebox1.mousedown += picturebox1_mousedown; picturebox1.mouseup += picturebox1_mouseup; picturebox1.mousemove += picturebox1_mousemove; picturebox1.paint += picturebox1_onpaint; } [dllimport("gdi32.dll")] static extern int setrop2(intptr hdc, int fndrawmode); [dllimport("gdi32.dll")] static extern intptr createpen(int fnpenstyle, int nwidth, uint crcolor); [dllimport("gdi32.dll")] static extern intptr selectobject(intptr hdc, intptr hgdiobj); [dllimport("gdi32.dll")] static extern bool deleteobject(intptr hobject); [dllimport("gdi32.dll")] static extern bool movetoex(intptr hdc, int x, int y, intptr lppoint); [dllimport("gdi32.dll")] static extern bool lineto(intptr hdc, int nxend, int nyend); private const int ps_solid = 0; private const int r2_maskpen = 9; private const int r2_copypen = 13; list<point> points = new list<point>(); private void drawhighlight(graphics g, point[] usepoints, int brushsize, color brushcolor) { int usecolor = system.drawing.colortranslator.towin32(brushcolor); intptr pen = createpen(ps_solid, brushsize, (uint)usecolor); intptr hdc = g.gethdc(); intptr xdc = selectobject(hdc, pen); setrop2(hdc, r2_maskpen); (int = 1; <= usepoints.length - 1; i++) { point p1 = usepoints[i - 1]; point p2 = usepoints[i]; movetoex(hdc, p1.x, p1.y, intptr.zero); lineto(hdc, p2.x, p2.y); } setrop2(hdc, r2_copypen); selectobject(hdc, xdc); deleteobject(pen); g.releasehdc(hdc); } private void picturebox1_onpaint(object sender, painteventargs e) { if (start) { base.onpaint(e); (int = 0; < list_points.count; i++) { drawhighlight(e.graphics, list_points[i].toarray(), 16, color.red); } } } private void picturebox1_mousedown(object sender, mouseeventargs e) { list_points.add(new list<point>()); start = true; is_mouse_down = true; } private void picturebox1_mouseup(object sender, mouseeventargs e) { count = count + 1; is_mouse_down = false; } private void picturebox1_mousemove(object sender, mouseeventargs e) { if (is_mouse_down == true) // check see if mouse button down while moving on form. { list_points[count].add(new point(e.x, e.y)); picturebox1.invalidate(); } } to answer question why not using graphics.drawline() , have add photo:
as can see...it weird jagged lines.

Comments
Post a Comment