问题描述
亲爱的Somewho:
我遇到有关C#语言的C#图片问题.
我正在尝试为我的学生做一些有关图片计算的程序.
像下面的例子
Dear Somewho:
I got a problem about C# image about C# language.
I am trying to do some program about picture calculation for my student.
example like the following
private void button2_Click(object sender, EventArgs e)
{
Bitmap b = pictureBox1.Image as Bitmap;
if (b == null)
{
MessageBox.Show("Input Image");
return;
}
int width = b.Width;
int height = b.Height;
// Lock Bitmap memory address
BitmapData bd = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
int stride = bd.Stride;
//Scan Image
IntPtr imgptr = bd.Scan0;
//counting Bytes
int widthByte = width * 3;
//Counting Padding bytes
int skipByte = stride - widthByte;
int[, ,] rgbData = new int[width, height, 3];
#region Read RGB
unsafe
{
byte* p = (byte*)(void*)imgptr;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
rgbData[i, j, 2] = p[0]; // Blue
p++;
rgbData[i, j, 1] = p[0]; // Green
p++;
rgbData[i, j, 0] = p[0]; // Red
p++;
}
p += skipByte; //Skip Padding bytes
}
}
#endregion
#region Gray Scalar
unsafe
{
imgptr = bd.Scan0;
byte* p = (byte*)(void*)imgptr;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
int gray = (rgbData[i, j, 0] + rgbData[i, j, 1] +
rgbData[i, j, 2]) / 3;
p[0] = (byte)gray; // Blue to Gray
p++;
p[0] = (byte)gray; // Green to Gray
p++;
p[0] = (byte)gray; // Red Gray
p++;
}
p += skipByte; //Skip Padding bytes
}
}
#endregion
b.UnlockBits(bd);
Refresh();
}
问题是我不知道如何计算像素位置.
例如:
要在该图片上输入一张为三角形或正方形图形的图片.
如何获得关于正方形[x1,y1] suqare [x2,y2]正方形[x3,y3]正方形[x4,y4]的正方形提示关节位置
因此很难使用g.drawline(x,y)来圈出4边的正方形长度.
如果可能的话,您介意告诉我如何获得方形4尖头位置的位置吗?
最好的问候,.
The problem is I don''t know how to calculate pixel position.
For example:
To input one picture which is a triangle or square graphics on this picture.
How can I get this square tips joint position about square [x1,y1] suqare[x2,y2] square[x3,y3] square[x4,y4]
so it''s hard to use g.drawline(x,y) to circulate out the square 4 side length.
If it is possible, would you mind to tell me about how to get the position for square 4 tips position?
Best Regards,.
推荐答案
private void myPictureBox_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.Yellow, new Rectangle(20, 20, 30, 30));
GraphicsPath p = new GraphicsPath();
p.AddLines(new Point[] { new Point(50, 50), new Point(30, 80), new Point(80, 80), new Point(50, 50) });
g.FillPath(Brushes.Red, p);
}
这将在图像上绘制一个简单的黄色正方形和一个红色三角形.
确定尺寸是基本的几何形状,并且非常简单,但是要取决于您的形状.
This will draw a simple yellow square and a red triangle on your image.
Working out the sizes is basic geometry, and pretty simple, but will depend on your shape.
这篇关于关于C#图像计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!