PDA

View Full Version : رسم خط در سی شارپ با استفاده از متد SetPixel از شی Bitmap



arash.gh
یک شنبه 04 اردیبهشت 1390, 02:43 صبح
دوستان با استفاده از متد زیر می خوام یک خط رسم کنم دستورات رو نوشتم اما error داره هرکاری کردم رفع نشد اساتید گرامی اگه edit کردن یا دستورات جدید گذاشتن یا error رو پیدا کردن ممنون می شم رهنمایی کنن .
با تشکر فراوان

public partial class Form1 : Form
{
Bitmap mybmp;
//ilag3=0;
int x1,x2,y1,y2,bold;
Color mycolor = System.Drawing.Color.Red;
//***********************
void line(int x1, int y1, int x2, int y2)
{
int step = 0, k, dx = x2 - x1, dy = y2 - y1;
float x, y, xinc = 0, yinc = 0;
if (Math.Abs(dx) > Math.Abs(dy))
step = Math.Abs(dx);
else
step = Math.Abs(dy);
if (step > 0)
{
xinc = (float)(dx) / step;
yinc = (float)(dy) / step;
}
x = x1;
y = y2;
for (k = 0; k <= step; k++)
{
mybmp.SetPixel(Convert.ToInt32(x), Convert.ToInt32(y), mycolor);
x+= xinc;
y+= yinc;
}
pictureBox1.Refresh();
}
//************************************************** **************

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

line(x1,y1,x2,y2);
//line(50, 70, 40, 90);
pictureBox1.Image = mybmp;

}

hassan_kahrizy
یک شنبه 04 اردیبهشت 1390, 15:25 عصر
بسمه تعالی
سلام
در لینک های زیر نحوه رسم خط وجود دارد
http://msdn.microsoft.com/en-us/library/aa287522%28v=vs.71%29.aspx
http://www.geekpedia.com/tutorial50_Drawing-with-Csharp.html

exlord
یک شنبه 04 اردیبهشت 1390, 15:50 عصر
private void button1_Click(object sender, EventArgs e)
{
mybmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = line(10, 10, 100, 100);
}
Bitmap mybmp;
Bitmap line(int x1, int y1, int x2, int y2)
{
double a = (y2 - y1) / (x2 - x1);
//y = ax + b
int b = (int)(y1 - (a * x1));

for (int x = x1; x <= x2; x+=2)
{
int y = (int)(a * x) + b;
mybmp.SetPixel(x, y, Color.Red);
}

return mybmp;
}