I'm gerenerally new to coding and i would like to seek some help to guide me about how to make a drawable triangle on the inkcanvas. I've created a button. Upon clicking on the button, i'm supposed to click in the inkcanvas and drag to form the triangle. (Work the same as any shapes to be drawn on the paint application in windows and microsoft words.)
I'm quite lost about how to go about doing this as i need to do a few other random shapes as well. I was hoping to be able to know how to create a triangle and use that knowledge i get from here to try to do the remaining other shapes that i will be doing.
Thanks so much for your time!
You can use mouse click event to catch the points -> Class: Point(x,y);
Then you can use DrawLine() to draw line between two Points.
E.g
Graphics g = e.Graphics;
Pen p = new Pen(Color.Red);
Point p1 = new Point(50,50);
Point p2 = new Point(1,1);
g.DrawLine(p, p1, p2);
g.Dispose();
Here are two good examples:
http://www.techotopia.com/index.php/Drawing_Graphics_in_C_Sharp
http://www.java2s.com/Code/CSharp/2D-Graphics/Drawshapestothebitmapinmemory.htm
Related
I have been working on creating a program similar to MS Paint. I have several of the features it has down but the one which is currently giving me trouble is the rectangular selection tool. My program currently draws everything on the panel and saves it all in an ArrayList so each shape can be redrawn in Paint().
Like MS paint I would like the user to be able to select a section of the drawing on the panel and either copy it, move it, re-size it, or even delete it. I was thinking about having the user draw a rectangle & saving the information for it. Then taking that information for the rectangle, passing them to create a new Bitmap. I would then paint a new rectangle in the background color to give the appearance that the selected area was "removed" when the selected portion is moved. It sounded okay until I realized that I couldn't use the Graphics.FromImage() on the PaintEventArgs variable passed to Paint() which made my idea useless. Not sure if that makes sense so my apologies if it's a confusing mess.
I've been searching the internet for some assistance and I haven't found much to help so either this is very easy to do, very difficult, or "rectangle selection tool" is not the proper term. Any help or pointers would be greatly appreciated!!! Thank you for your time! :)
I understand that you actually have the Rectangle and now would like to copy an area from your painted Panel.
This is possible, assuming you have, as you should, placed all the painting in the Paint event of the Panel.
Then you can, use DrawToBitmap to ask the Panel to draw itself onto a new Bitmap; from there you can DrawImage the Rectangle onto your Panel.
Note: For this to integrate with your list of 'Paint-Actions' you will have to either now store that Bitmap or store the Rectangle and redo the whole operation.
using (Graphics G = panelCanvas.CreateGraphics() )
{
Rectangle R0 = new Rectangle(22,22,55,55); // your Rectangle!
using (Bitmap bmp = new
Bitmap(panelCanvas.ClientSize.Width, panelCanvas.ClientSize.Height))
{ panelCanvas.DrawToBitmap(bmp, panelCanvas.ClientRectangle);
G.DrawImage(bmp, 111f, 111f, R0, GraphicsUnit.Pixel);
}
}
Aside: Please do replace the ArrayList, which is depracated by the new List<T>, e.g. a List<PaintAction> or whatever name your class has!
If you simply want to extract a rectanglular area from the Panel Control you can use thsi function:
public Bitmap getAreaFrom(Control ctl, Rectangle area)
{
Bitmap bmp2 = new Bitmap(area.Width, area.Height);
using (Graphics G = ctl.CreateGraphics())
using (Bitmap bmp = new Bitmap(ctl.ClientSize.Width, ctl.ClientSize.Height))
{
ctl.DrawToBitmap(bmp, ctl.ClientRectangle);
using (Graphics G2 = Graphics.FromImage(bmp2))
G2.DrawImage(bmp, 0f, 0f, area, GraphicsUnit.Pixel);
}
return bmp2;
}
I'm hoping someone can give me some guidance here. I have been gogleing for a while now and I can't come up with anything that suits my needs. I'm a bit of a programmer but not a pro and I have no graphics experience. I am trying to develop a program for my wife to more easily transfer images to her needlepoint drawings. I want to write a C# application that will let me load an image of almost any type and overlay a "grid" on top of it. I want to also be able to implement simple "paint" operations like change the color of a grid square, color selector from the base image, bucket fill, etc. Any suggestions and examples would be greatly appreciated.
Thanks,
Tom
I've implemented something similar for my wife. My basic approach:
1) Scale the image down to the number of necessary pixels. For example, if she's stitching the image on a 10x10 13-mesh canvas, that equates to an image of 130x130 pixels.
Here's some example code to start you off:
// use NearestNeighbor algorithm
public static unsafe Bitmap Reduce(Bitmap source, SizeF toSize, int threadCount)
{
Bitmap reduced = new Bitmap((int)(toSize.Width * threadCount), (int)(toSize.Height * threadCount));
using (Graphics g = Graphics.FromImage(reduced))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(source, new Rectangle(Point.Empty, reduced.Size));
}
return reduced;
}
2) Display the pixelated image full screen. This will provide a grid-like effect.
3) Display a color palette from DMC's yarn code card, or taken from the image (after down-scaling). Then have the mouse cursor pick up a color by clicking on it, then applying it to the cell that it was subsequently clicked on.
Here's some code for picking up the mouse cursor:
public Point GetPicturePointAtClick()
{
Point p = Cursor.Position;
Point picturePoint = previewBox.PointToClient(p);
if (Zoom != 0)
{
picturePoint.X = (int)(picturePoint.X / Zoom);
picturePoint.Y = (int)(picturePoint.Y / Zoom);
}
return picturePoint;
}
The idea here is to map the clicked area to the correct pixel in the reduced image, math:
Point reducedPoint =
new Point(
(int)(picPoint.X * (_reduced.Width / (float)WorkingBitmap.Width)),
(int)(picPoint.Y * (_reduced.Height / (float)WorkingBitmap.Height)));
There's a lot of code ahead of you. Did you try an online custom needlepoint provider? Try one of these sites, they're both pretty good and customization is free:
http://www.needlepaint.com/
http://www.pepitaneedlepoint.com/
A nice extension exists for ZedGraph to plot markers/points (PointObj.cs). However, I am having trouble rendering the point on the graph.
When I call the ZedGraph.Invalidate() function, the marker is drawn momentarily but then disappears.
In the following code, the variable zedGraph is the visible graph object on the form UI.
// Create point
ZedGraph.PointObj point = new ZedGraph.PointObj(5, 10000, 50, 50, ZedGraph.SymbolType.Square, Color.Green);
ZedGraph.PaneBase paneBase = zedGraph.GraphPane;
point.Fill = new ZedGraph.Fill(Color.Green);
System.Drawing.Graphics graphics = zedGraph.CreateGraphics();
// Draw point to graph
point.Draw(graphics, paneBase, paneBase.CalcScaleFactor());
// Re-draw graph, but point only flashes momentarily.
zedGraph.Invalidate();
EDIT: I realise there are other ways of adding "points", such as described here (Labelling and circle a specific point in zedgraph). But it would be still good to know why this doesn't work.
Try adding the point to the GraphObjList after creation
zedGraph.GraphPane.GraphObjList.Add(point);
I have converted postcode boundary polygons to point data (point[] for each polygon) from GIS Shape Files.
I am wanting to show this in a c# windows forms application.
I have managed to show this using the System.Drawing (GDI+) DrawPolygon() method.
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Black);
Brush brush = new SolidBrush(Color.FromArgb(255,255,o));
PointF[] ptr = { point data here };
g.FillPolygon(brush, ptr);
g.DrawPolygon(pen, ptr);
Is it possible to add events to a drawn polygon? If so how do I do this for individual polygons. For example, click on a postcode polygon and a messagebox shows information about the postcode.
Secondly, would it be easier to make a custom control inheriting the winforms panel. Is there a way to shape the border of a winforms panel control using a set of points?
Postcode objects are serialised and stored in the filesystem.
Using custom shaped controls will require a bit of tweaking, but when you get it right, Windows will take care of the hit tests for you.
If you choose to use the GDI+ approach, you'll want to draw the polygons in the Paint event handler of your form, and handle the MouseDown event to figure out in which polygon the coordinates fall into.
A "drawn" polygon is no longer an object, so there is no way to add events to it. You could however make a Polygon class with a method void PaintMe(Graphics g) and a method bool HitTest(int x, int y) so you have all logic in one class.
I have an idea and maybe you guys can give me a good start or an idea in which path might be correct.
I have a picturebox right now loading a specific bmp file. What I want to do is load this bmp file into the picturebox and then load another picture on top of it. The kicker to this all is the 2nd picture must be drawn. The 2nd picture is just a fill in black box. This black box must also overlay on the first image exactly right, the black box has cordinates from paint on it (yes we have the # of the cordaints).
Still think the picturebox is the way to go, or is there a way to load paint into this, and then paint on top of the paint image?
1) Need to load an image
2) Need to read a specific file that has cords
3) Need to draw a black rectangle that matches those coords (Those cords were created in paint).
What do you think the best way to approach this is? A Picture box with code to draw in the cords of the redacted image
Here's a code sample that should do what you're after:
//Load in an image
pbTest.Image = Image.FromFile("c:\\Chrysanthemum.jpg");
//Create the graphics surface to draw on
using (Graphics g = Graphics.FromImage(pbTest.Image))
{
using (SolidBrush brush = new SolidBrush(Color.Black))
{
//Draw a black rectangle at some coordinates
g.FillRectangle(brush, new Rectangle(0, 0, 20, 10));
//Or alternatively, given some points
//I'm manually creating the array here to prove the point, you'll want to create your array from your datasource.
Point[] somePoints = new Point[] { new Point(1,1), new Point(20,25), new Point(35, 50), new Point(90, 100) };
g.FillPolygon(brush, somePoints);
}
}
The finished article:
This answer is written to apply to both web and non-web uses of C# (why I did not give specific examples.)
GDI and other graphics libs all have functions that will paint a filled rectangle on top of an image. This is the way to go. If you use two images there is a good chance for a standard user and a great chance for a hacker they will be able to view just the original image, exposing the information you are trying to hide.
If you only send an image with the areas redacted, you will never have to worry about them being seen.