提高我的C#位图的分辨率
我正在使用位图在WinForm中创建自定义图像。我有一个表示桁架的类,并希望将其可视化。现在,这是我绘制桁架的代码:提高我的C#位图的分辨率,c#,image,winforms,bitmap,C#,Image,Winforms,Bitmap,我正在使用位图在WinForm中创建自定义图像。我有一个表示桁架的类,并希望将其可视化。现在,这是我绘制桁架的代码: public void DrawAnsComponent() { Pen pen = new Pen(Color.Black); maxWidth = 0; maxHeight = 0; //Getting size of bitmap foreach (AnsJoint joint
public void DrawAnsComponent()
{
Pen pen = new Pen(Color.Black);
maxWidth = 0;
maxHeight = 0;
//Getting size of bitmap
foreach (AnsJoint joint in this.AnsToShow.AnsJoints)
{
if (joint.Location.X.Length > maxWidth)
{
maxWidth = (int)joint.Location.X.Length;
}
if (joint.Location.Y.Length > maxHeight)
{
maxHeight = (int)joint.Location.Y.Length;
}
}
maxHeight += 55; maxWidth += 5;
Bitmap bm = new Bitmap(maxWidth, maxHeight); //Creating Bitmap
gr = Graphics.FromImage(bm); //Creating graphic to project onto bitmap
gr.SmoothingMode = SmoothingMode.HighQuality;
foreach (AnsJoint joint in this.AnsToShow.AnsJoints)
{
PointF jointPoint = this.ToCartesian(new PointF((float)joint.Location.X.Length - 4f, (float)joint.Location.Y.Length + 10f), maxHeight);
gr.DrawString(joint.JointID.ToString(), new Font(FontFamily.GenericMonospace, 6f, FontStyle.Regular, GraphicsUnit.Point, 1, false), Brushes.Black, jointPoint);
}
foreach (AnsMember member in this.AnsToShow.AnsMembers) //Drawing each member
{
List<AnsPanel> panels = member.Panels; //Drawing the panels
foreach (AnsPanel pan in panels)
{
pen.Color = Color.Red;
PointF p1 = this.ToCartesian(new PointF((float)pan.I.Location.X.Length, (float)pan.I.Location.Y.Length), maxHeight);
PointF p2 = this.ToCartesian(new PointF((float)pan.J.Location.X.Length, (float)pan.J.Location.Y.Length), maxHeight);
gr.DrawEllipse(pen, p1.X - 2.5f, p1.Y - 2.5f, 5, 5);
gr.DrawEllipse(pen, p2.X - 2.5f, p2.Y - 2.5f, 5, 5);
/*
gr.DrawEllipse(pen, p1.X - 3, p1.Y - 3.3f, 5, 5);
gr.DrawEllipse(pen, p2.X - 3, p2.Y - 3.3f, 5, 5);
pen.Color = Color.Black;
gr.DrawLine(pen, p1, p2);
*/
}
List<AnsLink> links = member.Links; //Drawing the links
foreach (AnsLink link in links)
{
PointF p1 = this.ToCartesian(new PointF((float)link.I.Location.X.Length, (float)link.I.Location.Y.Length), maxHeight);
PointF p2 = this.ToCartesian(new PointF((float)link.J.Location.X.Length, (float)link.J.Location.Y.Length), maxHeight);
gr.FillEllipse(Brushes.Green, p1.X - 1.5f, p1.Y - 1.5f, 3, 3);
gr.FillEllipse(Brushes.Green, p2.X - 1.5f, p2.Y - 1.5f, 3, 3);
gr.DrawLine(pen, p1, p2);
}
}
pictureBox1.Image = bm;
public PointF ToCartesian(PointF p, int maxHeight)
{
return new PointF(p.X, (p.Y - (maxHeight * .8f)) * -1);
}
public void DrawAnsComponent()
{
钢笔=新钢笔(颜色为黑色);
最大宽度=0;
最大高度=0;
//获取位图的大小
foreach(此.AnsToShow.AnsJoints中的AnsJoint关节)
{
如果(joint.Location.X.Length>maxWidth)
{
maxWidth=(int)joint.Location.X.Length;
}
如果(关节位置Y长度>最大高度)
{
maxHeight=(int)joint.Location.Y.Length;
}
}
最大高度+=55;最大宽度+=5;
位图bm=新位图(maxWidth,maxHeight);//创建位图
gr=Graphics.FromImage(bm);//创建要投影到位图上的图形
gr.SmoothingMode=SmoothingMode.HighQuality;
foreach(此.AnsToShow.AnsJoints中的AnsJoint关节)
{
PointF jointPoint=this.ToCartesian(新的PointF((float)joint.Location.X.Length-4f,(float)joint.Location.Y.Length+10f),最大高度);
gr.DrawString(joint.JointID.ToString(),新字体(FontFamily.GenericMonospace,6f,FontStyle.Regular,GraphicsUnit.Point,1,false),Brush.Black,jointPoint);
}
foreach(this.AnsToShow.AnsMembers中的AnsMember成员)//绘制每个成员
{
名单
因此,它工作得非常好,只是像素化使它看起来像是一张非常低质量的图片。我的代码有什么可以改变的吗,以使图像质量更高?PictureBox
只不过是放大你在位图中绘制的小图像。你显然是在确定大小,以像素为单位,根据“真实世界”中的尺寸绘制桁架的坐标。您可以在计算几何图形的屏幕坐标时应用比例因子,以便图形填充控件的可见空间。但是,如果这样做,我建议将位图
切掉,而直接在画框
中的画框
上绘制事件处理程序(或派生自定义控件并将图形放入受保护的OnPaint
方法)。这样你就不必处理位图
的大小与PictureBox
的大小保持同步的问题,这将是一个非常麻烦的问题,更不用说效率低下了。基本上,你不希望PictureBox
尝试调整图像大小,因为它会给你带来放大时所看到的那种模糊和怪异缩小比例时会出现瑕疵
希望这篇文章足够清晰、内容丰富,可以让您开始学习。两个想法:
1) Graphics
对象上还有一些其他属性,您需要使用这些属性,包括和
2) 您可能会使位图比您实际想要的小得多。一个允许您在不明显更改代码的情况下使位图变大的方法是使用缩放因子,例如
var scale = 2;
Bitmap bm = new Bitmap(maxWidth * scale, maxHeight * scale); //Creating Bitmap
using (var gr = Graphics.FromImage(bm)) //Creating graphic to project onto bitmap
{
gr.Transform.Scale(scale, scale)
// continue as before
}
另外,不要忘了处理一次性对象(如图形对象)。在本例中,使用关键字可以为您解决这个问题。非常感谢!我已经基本解决了我的问题,除了我在这里描述的一件事,修复了除缩放不正常外的问题。
var scale = 2;
Bitmap bm = new Bitmap(maxWidth * scale, maxHeight * scale); //Creating Bitmap
using (var gr = Graphics.FromImage(bm)) //Creating graphic to project onto bitmap
{
gr.Transform.Scale(scale, scale)
// continue as before
}