Here is my code and I want to implement DDA algorithm without using drawLine method in c# . I tried to use PutPixel method but it did not work . There is nothing in my window. Is there any way to draw line without using drawLine method in c# ?
private void Form1_Paint(object sender, PaintEventArgs e)
{
grafik = e.Graphics;
DDACiz(ilk.X, ilk.Y, ikinci.X, ikinci.Y, grafik, Color.DarkRed);
}
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
bm.SetPixel(0, 0, Color.DarkRed);
g.DrawImageUnscaled(bm, x, y);
}
void DDACiz(int x1, int y1, int x2, int y2,Graphics grafik, Color renk)
{
int PikselSayisi;
int dx, dy;
float x, xFark;
float y, yFark;
dx = x2 - x1;
dy = y2 - y1;
PikselSayisi = Math.Abs(dx) > Math.Abs(dy) ? Math.Abs(dx) : Math.Abs(dy);
xFark = (float)dx / (float)PikselSayisi;
yFark = (float)dy / (float)PikselSayisi;
x = (float)x1;
y = (float)y1;
while (PikselSayisi!=0)
{
PutPixel(grafik,(int)Math.Floor(x + 0.5F),(int) Math.Floor(y + 0.5f),renk);
x += xFark;
y += yFark;
PikselSayisi--;
}
}
}
}
There is no Graphics.DrawPoint method, so to draw a single pixel with a Graphics object you need to use Graphics.FillRectangle
Change
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
bm.SetPixel(0, 0, Color.DarkRed);
g.DrawImageUnscaled(bm, x, y);
}
to
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
g.FillRectangle(Brushes.DarkRed, x, y, 1, 1);
}
or if you want to use the Color c:
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
using (SolidBrush brush = new SolidBrush(c) )
g.FillRectangle(brush , x, y, 1, 1);
}
You could also use your appoach of drawing a bitmap but you need to make it either 1x1 pixels wide or make sure it is transparent and also use CompositingMode.SourceOver.
Writing a line draw method is an interesting exercise; much harder than it seems and really tough for PenWidths other than 1.0, let alone for alpha channels other than fully opaque..
Related
i have a program which takes a screen shot of the selected area (which I select with a mouse) and saves it to a clipboard. The problem is it works only if i make a selection from top to bottom. If I try to make a selection in any other direction (bottom to top, right to left, left to right) the program crashes. This is the code for MouseMove:
public void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (this.isMouseDown)
{
double curx = e.GetPosition(null).X;
double cury = e.GetPosition(null).Y;
System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
SolidColorBrush brush = new SolidColorBrush(Colors.White);
r.Stroke = brush;
r.Fill = brush;
r.StrokeThickness = 1;
r.Width = Math.Abs(curx - x);
r.Height = Math.Abs(cury - y);
selekt.Children.Clear();
selekt.Children.Add(r);
Canvas.SetLeft(r, x);
Canvas.SetTop(r, y);
if (e.LeftButton == MouseButtonState.Released)
{
selekt.Children.Clear();
width = e.GetPosition(null).X - x;
height = e.GetPosition(null).Y - y;
this.CaptureScreen(x, y, width, height);
this.x = this.y = 0;
this.isMouseDown = false;
this.Close();
}
}
}
And this is for CaptureScreen:
public void CaptureScreen(double x, double y, double width, double height)
{
int ix, iy, iw, ih;
ix = Convert.ToInt32(x);
iy = Convert.ToInt32(y);
iw = Convert.ToInt32(width);
ih = Convert.ToInt32(height);
Bitmap slika = new Bitmap(iw, ih, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(slika);
g.CopyFromScreen(ix, iy, 0, 0,new System.Drawing.Size(iw, ih),CopyPixelOperation.SourceCopy);
System.Windows.Forms.Clipboard.SetImage(slika);
are you getting error the below mentioned code? That seems to be the place where you would get one.
Canvas.SetLeft(r, x);
Canvas.SetTop(r, y);
If yes, then thats because SetLeft takes UIElement and a double values
public static void SetLeft(
UIElement element,
double length)
And also, I am guessing that x and y are double and declared public.
I am trying to create textboxes and draw circles dynamically within a user control. The circle is visible but the textboxes are not visible when I run my application. Am I missing something in the code?
Please find the code below,
public partial class uscCircle : UserControl
{
public uscCircle()
{
InitializeComponent();
}
public void DrawCircle(PaintEventArgs args, int x, int y, int width, int height)
{
Pen pen = new Pen(Color.Red, 3);
Brush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
args.Graphics.FillEllipse(myBrush, x - width / 2, y - height / 2, width, height);
}
public void AddTextBox(string text, int x, int y, int width, int height)
{
markerlabel.Size = new Size(40, 15);
markerlabel.Text = text;
markerlabel.TextAlign = HorizontalAlignment.Center;
markerlabel.BorderStyle = BorderStyle.FixedSingle;
markerlabel.ForeColor = Color.White;
markerlabel.BackColor = Color.Red;
markerlabel.Location = new Point(x - (width + 14), y + height / 2);
markerlabel.Visible = true;
this.Controls.Add(markerlabel);
}
}
public partial class CalibrationForm : Form
{
private CalibrationForm_Click(object sender, EventArgs e)
{
int x = e.X;
int y = e.Y;
DrawTextBox(X, Y, 25, 25, "1234", "abcd");
}
private void DrawCircle(int x, int y, int width, int height, string MarkerID, string type)
{
PaintEventArgs arg = new PaintEventArgs(this.CreateGraphics(), new Rectangle());
uscCircle circle = new uscCircle();
circle.DrawCircle(arg, x, y, width, height);
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x + 40, y, width, height);
}
}
I don't see where you add uscCircle to the form. If that isn't displayed, neither will the textbox be.
Such as:
private void DrawCircle(int x, int y, int width, int height, string MarkerID, string type)
{
uscCircle circle = new uscCircle();
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x+40, y, width, height);
this.Controls.Add(circle);
}
You are adding your TextBox into a new User control which is then not used.
uscCircle circle = new uscCircle();
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x+40, y, width, height);
You either need to add your 'uscCircle' into the controls of the Form
this.Controls.Add(uscCircle); // Must be in your Form file
or you move the TextBox-Generation code into your form
I have data like this(x_pos, y_pos, z_pos, Length, height, depth). I need to draw cube by using this information. Can any one help me to do draw Teechart for this. give me some sample code for this.
Yes, you can do it with custom canvas drawing using the Cube(Rectangle r, int z0, int z1) method in TChart's AfterDraw event, for example:
void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
int x = 100;
int y = 100;
int z = 0;
int width = 50;
int height = 50;
int depth= 10;
DrawCube(x, y, z, width, height, depth, g);
}
private void DrawCube(int x, int y, int z, int width, int height, int depth, Steema.TeeChart.Drawing.Graphics3D g)
{
Rectangle r = new Rectangle(x, y, width, height);
g.Cube(r, z, z + depth);
}
I am working on a small project and packing it into GUI. The reference source code is DrawTools(Download source code - 61.1 Kb).
The reference source code demos a drawing tool in C# WinForms.
The function is to draw different figures like rectangle, ellipse, polygon, etc.
I want to use the location and size information of these figures to do further work, so if I draw a rectangle in the draw area, could C# WinForms returns the parameter of this figure(eg. x,y,width,height in the DrawRectangle.cs)?
Code as follow:
public DrawRectangle(int x, int y, int width, int height)
{
rectangle.X = x;
rectangle.Y = y;
rectangle.Width = width;
rectangle.Height = height;
Initialize();
}
Further more, How to get the returned parameters and then displayed in a new dialog?
when you draw these shapes: rectangle, ellipse, polygon, etc. you are using the location and width and height of them. if you want to save them to an object create one and save them in a list of some other structure...
for example:
List<object> shapes = new List<object>();
private void drawSquare(int x1, int y1, int x2, int y2)
{
shapes.Add(new Rectangle(x1, y1, x2, y2));
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
foreach (var shape in shapes)
{
if (shape is Rectangle)
{
g.DrawRectangle(new Pen(Color.Black), (Rectangle)shape);
}
}
}
this is just a small example, you should check the OnPaint method and Graphics to get more information on what you can and should do
You can add some event to support notifying what is happening, something like this:
public class InitRectangleEventArgs : EventArgs {
public Rectangle Rectangle {get;set;}
}
public delegate void InitRectangleEventHandler(object sender, InitRectangleEventArgs e);
public event InitRectangleEventHandler InitRectangle;
public DrawRectangle(int x, int y, int width, int height)
{
rectangle.X = x;
rectangle.Y = y;
rectangle.Width = width;
rectangle.Height = height;
if(InitRectangle != null) InitRectangle(this, new InitRectangleEventArgs { Rectangle = new Rectangle(x,y,width,height)});
Initialize();
}
//To use it, just subscribe the event so that you can know the
//info of the Rectangle everytime it is initialized
InitRectangle += (s,e) => {
//Get the info from the Rectangle property of e: e.Rectangle
//....
};
I often need to draw items in a Graphics object and the way I've been doing it is to have a function DrawItem that receives the Graphics object and an offsetX and offsetY parameters, which determine at which point the item will be drawn.
The problem is that the code inside DrawItem would look a lot better if there would be a method in Graphics that would give me a version of the Graphics where the X and Y axis zeroes are at some other point, something like myGraphics.DisplacedGraphics(offsetX, offsetY). This way I would just pass this Graphics objects to my DrawItem method which wouldn't need to receive the other two parameters. Is there such function or what's the closest thing?
Edit: On the meanwhile this is what I wrote, but seems like such a basic requirement I still hope there already exists such functionality (I'd still need to add a bunch of methods but these are all I need for now) (note the DisplacedCanvas method):
public class Canvas
{
private readonly Graphics _Graphics;
private readonly int _OriginX = 0;
private readonly int _OriginY = 0;
public Canvas(Graphics graphics, int originX, int originY)
{
_Graphics = graphics;
_OriginX = originX;
_OriginY = originY;
}
public Canvas(Graphics graphics) : this(graphics, 0, 0) { }
public SizeF MeasureString(string text, Font font)
{
return _Graphics.MeasureString(text, font);
}
public void FillRectangle(Brush brush, int x, int y, int width, int height)
{
_Graphics.FillRectangle(brush, _OriginX + x, _OriginY + y, width, height);
}
public void DrawString(string s, Font font, Brush brush, float x, float y)
{
_Graphics.DrawString(s, font, brush, _OriginX + x, _OriginY + y);
}
public Canvas DisplacedCanvas(int x, int y)
{
return new Canvas(_Graphics, _OriginX + x, _OriginY + y);
}
}
I'm pretty sure that the TranslateTransform() method will do what you're asking for.
The origin is typically the upper-left-hand corner of the drawing surface. The translation operation consists of multiplying the transformation matrix by a matrix whose translation part is the dx and dy parameters. This method applies the translation by prepending the translation matrix to the transformation matrix.
So if you want the new origin to be at 100, 50, then you would first call graphics.TranslateTransform(100, 50) before drawing your image.