标题:c# 中怎样把 在 PictureBox 中画好的图形保存为图片文件(如 jpg,bmp,gif)
只看楼主
daren_zj
Rank: 1
等 级:新手上路
帖 子:3
专家分:8
注 册:2013-4-8
结帖率:0
已结贴  问题点数:20 回复次数:1 
c# 中怎样把 在 PictureBox 中画好的图形保存为图片文件(如 jpg,bmp,gif)
private void button1_Click(object sender, EventArgs e)
        {
            int tem_Line = 0;
            int circularity_W = 4;
            if (pictureBox1.Width >= pictureBox1.Height)
                tem_Line = pictureBox1.Height;
            else
                tem_Line = pictureBox1.Width;
            //
            Rectangle rect = new Rectangle(circularity_W, circularity_W, tem_Line - circularity_W * 2, tem_Line - circularity_W * 2);
            Font star_Font = new Font("Arial", 30, FontStyle.Regular);
            string star_Str = "★";
            Graphics g = this.pictureBox1.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.Clear(Color.White);
            Pen myPen = new Pen(Color.Red, circularity_W);
            g.DrawEllipse(myPen, rect);
            SizeF Var_Size = new SizeF(rect.Width, rect.Width);
            Var_Size = g.MeasureString(star_Str, star_Font);
            g.DrawString(star_Str,star_Font,myPen.Brush,new PointF((rect.Width / 2F+circularity_W-Var_Size.Width/2F),(rect.Height / 2F -Var_Size.Width / 2F)));
            Font Var_Font = new Font("楷体", 20, FontStyle.Regular);
            
            Var_Size = g.MeasureString("专用章", Var_Font);
            g.DrawString("专用章", Var_Font, myPen.Brush, new PointF((rect.Width / 2F) + circularity_W - Var_Size.Width / 2F, rect.Height / 2F + Var_Size.Height * 2));
            string tempStr = "四川省万源市金属回收公司";
            int len = tempStr.Length;
            float angle = 180 + (180 - len * 20) / 2;
            for (int i = 0; i < len; i++)
            {
                g.TranslateTransform((tem_Line + circularity_W / 2) / 2, (tem_Line + circularity_W / 2) / 2);
                g.RotateTransform(angle);
                Brush myBrush = Brushes.Red;
                g.DrawString(tempStr.Substring(i, 1), Var_Font, myBrush, 60, 0);
                g.ResetTransform();
                angle += 20;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem == null)
            {
                return;
            }
            else
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Title = "保存为:";
                saveFileDialog.OverwritePrompt = true;
                saveFileDialog.CheckPathExists = true;
                saveFileDialog.Filter = comboBox1.Text + "|" + comboBox1.Text;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string fileName = saveFileDialog.FileName;
                    Bitmap bitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
                    bitmap.Save(fileName, ImageFormat.Jpeg);
                }
            }
        }


我刚开始学习C#,c#中怎样把 在PictureBox中画好的图形保存为图片文件(如jpg,bmp,gif),我是用GDI+画的,但是保存不了,保存后是一张空白的图片,求救高手指点!
搜索更多相关主题的帖子: private 图片 
2014-07-15 18:14
何事惊慌
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:220
专家分:499
注 册:2008-7-2
得分:20 
string fileName = saveFileDialog.FileName;
                    Bitmap bitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
                    bitmap.Save(fileName, ImageFormat.Jpeg);


bitmap这有对应的图片信息吗?你好像只是初始化了她的大小,但是这个内容是空的啊,你直接保存pictureBox1或者给这对应的bitmap赋值才行吧

QQ:860234001
编程交流群:236949758
2014-07-15 23:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-434076-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.470675 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved