【问题标题】:Increasing the resolution of my C# bitmap提高我的 C# 位图的分辨率
【发布时间】:2016-08-03 16:52:24
【问题描述】:

我正在使用位图在我的 WinForm 中创建自定义图像。我有一个代表桁架的类并希望将其可视化。现在这是我绘制桁架的代码:

    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);
    }

这是结果

所以它工作得非常好,除了像素化使它看起来像一张质量非常低的图片。有什么我可以更改我的代码以使图像质量更高的地方吗?

【问题讨论】:

标签: c# image winforms bitmap


【解决方案1】:

PictureBox 只是放大您在Bitmap 中绘制的小图像。您显然是从您的桁架的“真实世界”坐标中的大小确定绘图的大小(以像素为单位)。相反,您可以在计算几何图形的屏幕坐标时应用比例因子,以便绘图填充控件的可见空间。但是,如果您这样做,我建议放弃Bitmap,而是直接在PictureBoxPaint 事件处理程序中绘图(或派生自定义控件并将绘图放在受保护的OnPaint 方法中)。这样您就不必处理保持Bitmap 的大小与PictureBox 的大小同步的问题,这将是一个真正的麻烦,更不用说效率低下了。基本上,您不希望 PictureBox 尝试调整图像大小,因为它会给您在放大时看到的那种模糊和在缩小时奇怪的伪影。

希望这足够清晰和信息丰富,可以帮助您入门。

【讨论】:

【解决方案2】:

两个想法:

1) Graphics 对象上还有一些其他属性可以让您使用,包括 SmoothingModeInterpolationMode

Graphics.SmoothingMode = SmoothingMode.AntiAlias
Graphics.InterpolationMode = InterpolationMode.Bicubic

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
}

另外,不要忘记处置一次性对象(例如 Graphics 对象)。在此示例中,using 关键字为您处理。

【讨论】:

    猜你喜欢
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 2010-10-19
    • 2013-08-14
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多