简体   繁体   English

从图片框保存到文件

[英]Save to File from picturebox

The PictureBox has Image on it, Barcode PictureBox 上有图像,条形码

how to save to image file from PictureBox如何从PictureBox保存到图片文件

this is the button to generate BARCODE这是生成条形码的按钮

private void Generate_Barcode_Click(object sender, EventArgs e)
{
    string barcode = textBox1.Text;
    Bitmap bitmap = new Bitmap(barcode.Length * 40, 150);
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        Font ofont = new System.Drawing.Font("IDAutomationHC39M Free Version", 20);
        PointF point = new PointF(2f, 2f);
        SolidBrush black = new SolidBrush(Color.Black);
        SolidBrush white = new SolidBrush(Color.White);
        graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
        graphics.DrawString("*" + barcode + "*", ofont, black, point);
    }
    using (MemoryStream ms = new MemoryStream())
    {
        bitmap.Save(ms, ImageFormat.Png);
        Barcode_Result.Image = bitmap;
        Barcode_Result.Height = bitmap.Height;
        Barcode_Result.Width = bitmap.Width;
    }
}

Save the pictures drawn by PictrueBox to the specified folder.将PictrueBox绘制的图片保存到指定文件夹。

Idea: Do not draw the picture directly in the PictrueBox, but draw it in the Image property of the PictrueBox, and then refresh the display.思路:不要直接在PictrueBox中绘制图片,而是在PictrueBox的Image属性中绘制,然后刷新显示。

For detailed operation method, please refer to the following code:详细操作方法请参考以下代码:

    private void button1_Click(object sender, EventArgs e)
    {
        string barcode = textBox1.Text;
        Bitmap bitmap = new Bitmap(barcode.Length * 40, 150);
        pictureBox1.Image = bitmap;
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            Font ofont = new System.Drawing.Font("IDAutomationHC39M Free Version", 20);
            PointF point = new PointF(2f, 2f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush white = new SolidBrush(Color.White);
            graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
            graphics.DrawString("*" + barcode + "*", ofont, black, point);
            pictureBox1.Invalidate(); // refresh
            bitmap.Save(@"xxx:/your path/temp.bmp");
        }
   }

test code:测试代码:

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM