绘制并保存图像

时间:2018-11-22 08:29:15

标签: c#

我正在开发一个应用程序,该应用程序稍后将与我之前开发的另一个应用程序集成。

基本上,此应用将生成X和Y尺寸的图像文件,并在其上打印一个网格,用户还可以指定其间隔。

到目前为止,我已经完成了此表格,但是我很难确定用适当的尺寸和网格间距生成实际图像的最佳方法是什么。

我不想保存显示在表单上的图像,因为它只是一种表示形式,很可能与最终产品完全不同。

所以我想我的问题是,您认为当我单击“保存”时生成黑白图像的最佳方法是什么?

此外,我不需要查看正在保存的图像,我只想生成并保存在后台。

这是“绘制”按钮的点击事件

private void btnDraw_Click(object sender, EventArgs e)
{
    this.Width = 560;
    using (g = pb.CreateGraphics())
    {
        g.Clear(Color.White);

        PaintCanvass canvass = new PaintCanvass();
        canvass.Width = Convert.ToDouble(tbWidth.Text);
        canvass.Height = Convert.ToDouble(tbHeight.Text);
        canvass.Resolution = Convert.ToInt32(cbResolution.Text.Substring(0,3));
        canvass.UOM = cbUOM.Text;
        canvass.Interval = Convert.ToInt32(tbInterval.Text);

        Pen pencil = new Pen(Color.Black, 2);
        int hpfact = Convert.ToInt32((double)pb.Height / (double)canvass.horizontalLinesQuantity);
        int hp = hpfact;

        for (int i = 0; i < canvass.horizontalLinesQuantity-1; i++)
        {
            g.DrawLine(pencil, new Point(0, hp), new Point(pb.Width, hp));
            hp = hp + hpfact;
        }
        int vpfact = Convert.ToInt32((double)pb.Width / (double)canvass.verticalLinesQuantity);
        int vp = vpfact;
        for (int i = 0; i < canvass.verticalLinesQuantity-1; i++)
        {
            g.DrawLine(pencil, new Point(vp, 0), new Point(vp, pb.Height));
            vp = vp + vpfact;
        }
        canvass = null;

这是我的PaintCanvass类,现在看来好像只是作为属性的容器

   class PaintCanvass
    { 
        public double Width { get; set; }
        public double Height { get; set; }
        public string UOM { get; set; }
        public int Resolution { get; set; }
        public int Interval { get; set; } = 50;
        public int hdots
        {
            get
            {
                if (this.UOM == "mm")
                {
                    return Convert.ToInt32(Width * 0.03937008F * Resolution);
                }
                else
                {
                    return Convert.ToInt32(Width * Resolution);
                };
            }
        }
        public int vdots
        {
            get
            {
                // Set the quantity of lines
                if (this.UOM == "mm")
                {
                    return Convert.ToInt32(Height * 0.03937008F * Resolution);
                }
                else
                {
                    return Convert.ToInt32(Height * Resolution);
                };
            }
        }
        public int horizontalLinesQuantity
        {
            get
            {
                return vdots / this.Interval;
            }
        }
        public int verticalLinesQuantity
        {
            get
            {
                return hdots / this.Interval;
            }
        }
    }

Current Form Sample

编辑:按照建议,我使用了位图方法。

private void btnSave_Click(object sender, EventArgs e)
{
    SetupCanvass();
    using (Bitmap bmp = new Bitmap(canvass.hdots, canvass.vdots))
    {
        using (Graphics g = Graphics.FromImage(bmp))
        {
            using (SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255)))
            {
                g.FillRectangle(brush, 0, 0, canvass.hdots, canvass.vdots);
            }
            int hp = canvass.Interval;
            for (int i = 0; i < canvass.horizontalLinesQuantity - 1; i++)
            {
                g.DrawLine(pencil, new Point(0, hp), new Point(canvass.hdots, hp));
                hp = hp + canvass.Interval;
            }
            int vp = canvass.Interval;
            for (int i = 0; i < canvass.verticalLinesQuantity - 1; i++)
            {
                g.DrawLine(pencil, new Point(vp, 0), new Point(vp, canvass.vdots));
                vp = vp + canvass.Interval;
            }
        }
        bmp.Save(Path.Combine(Path.GetTempPath(), "labelGrid.png")); //Save image somwhere on disk
    }
}

1 个答案:

答案 0 :(得分:0)

如果要保存图像而不显示预览,请执行以下操作:

using (Bitmap bmp = new Bitmap(500, 500))
{
    using (Graphics g = Graphics.FromImage(bmp))
    {
        //Draw your stuff directly onto the bitmap here
    }
    bmp.Save("C:\\image.bmp"); //Save image somwhere on disk
}
相关问题