pictrueBox带保存图片

来源:互联网 发布:做字体的软件 编辑:程序博客网 时间:2024/04/27 21:08
 为了保存对pictruebox的image的操作,使用bitmap传值,达到效果。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WindowsFormsApplication2
{
    public partial class Form4 : Form
    {
        Pen p = new Pen(Color.Red, 2f);
        Graphics g = null;
        List<Point> line = new List<Point>();//存储线
        List<Point> points = new List<Point>();//存储点
        int t = 0;//打点递增
        double length = 0;//线长
        int pointcount = 0;//点的数量
        Bitmap newmap;

        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {     
            OpenFileDialog openfileDialog = new OpenFileDialog();
            openfileDialog.Filter = "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|" +
                "JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF files (*.gif)|*.gif |BMP files (*.b" +
                "mp)|*.bmp|Tiff files (*.tif;*.tiff)|*.tif;*.tiff|Png files (*.png)| *.png |All f" +
                "iles (*.*)|*.*";
            if ((openfileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK))
            {
                newmap = new Bitmap(Image.FromFile(openfileDialog.FileName.ToString()),Convert.ToInt32(pictureBox1.Width), Convert.ToInt32(pictureBox1.Height));
                this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = newmap;
                g = Graphics.FromImage(pictureBox1.Image);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            length = 0;
            if (e.Button == MouseButtons.Left)
            {  
                if (line.Count > 0 )
                {
                    line.Clear();
                    t = 0;
                }
                line.Add(new Point(e.X, e.Y));
            }        
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {              
                line.Add(new Point(e.X,e.Y));
                length += Math.Sqrt(Math.Pow((e.X - line[line.Count()-2].X), 2) + Math.Pow((e.Y - line[line.Count()-2].Y), 2));
                if (line.Count > 1)
                {
                    g.DrawLine(p, line[line.Count - 2].X, line[line.Count - 2].Y, line[line.Count - 1].X, line[line.Count - 1].Y);
                    pictureBox1.Image = newmap;
                }          
            }        
        }

        public void drawPoints(List<Point> pline, int nun, PaintEventArgs e)
        {
            double step = 0; 
            double len = 0;
            Point p = new Point();
            if (nun == 1)
            {
                p = pline[0];
                 t++;
                 drawPoint(p, e);
                 if (e == null)
                 {
                     points.Add(p);
                 }
            }
            else
            {
                step = length /(nun - 1);
                for (int i = 0; i < pline.Count(); i++)
                {

                    if (i != pline.Count() - 1)
                    {
                        len += Math.Sqrt(Math.Pow((pline[i + 1].X - pline[i].X), 2) + Math.Pow((pline[i + 1].Y - pline[i].Y), 2));
                    }
                    if (i == pline.Count() - 1 || i == 0)
                    {
                        p = pline[i];
                        t++;
                        drawPoint(p, e);
                        if (e == null)
                        {
                            points.Add(p);
                        }
                    }
                    else if (len >= step)
                    {
                        p = pline[i - 1];
                        t++;
                        drawPoint(p, e);
                        if (e == null)
                        {
                            points.Add(p);
                        }
                        len = 0;
                    }
                }
            }
            pictureBox1.Image = newmap;
        }

        public void drawPoint(Point pl, PaintEventArgs e)
        {
            if (e == null)
            {
                Font drawFont = new Font("Arial", 11, FontStyle.Bold);
                g.DrawString(t.ToString(), drawFont, new SolidBrush(Color.Lime), pl.X-10, pl.Y-8);
                g.DrawEllipse(new Pen(Color.Blue, 2), pl.X - 10, pl.Y - 10, 20, 20);
            }
            else
            {
                Font drawFont = new Font("Arial", 11, FontStyle.Bold);
                e.Graphics.DrawString(t.ToString(), drawFont, new SolidBrush(Color.Lime), pl.X - 10, pl.Y - 8);
                e.Graphics.DrawEllipse(new Pen(Color.Blue, 2), pl.X - 10, pl.Y - 10, 20, 20);
            }
            pictureBox1.Image = newmap;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pointcount = Int32.Parse(this.comboBox1.Text);
            drawPoints(line, pointcount,null);
        }

        float zhi = 100;
        private void button3_Click(object sender, EventArgs e)
        {
            foreach (Point po in points)
            {
                showResult(po,zhi);
            }
        }

        public void showResult(Point p,float testvar)
        {
            float te = 0;
            te = testvar / 2;
            SolidBrush sb4 = new SolidBrush(Color.FromArgb(60,Color.Red));
            g.FillEllipse(sb4, p.X-te, p.Y-te, testvar, testvar);
            pictureBox1.Image = newmap;
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string savePath = null;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.RestoreDirectory = true;//打开上一次所打开的目录
            if(saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                 savePath = saveFileDialog1.FileName.ToString();
            }
            Bitmap bt = new Bitmap(pictureBox1.Image);
            JpegSave(bt, savePath, 1000000L);
        }

        //========================保存图片==========================
        public void JpegSave(Bitmap b, string name, long quality)
        {
            ImageCodecInfo myImageCodecInfo;
            System.Drawing.Imaging.Encoder myEncoder;
            EncoderParameter myEncoderParameter;
            EncoderParameters myEncoderParameters;
            myImageCodecInfo = GetEncoderInfo("image/jpeg");
            myEncoder = System.Drawing.Imaging.Encoder.Quality;
            myEncoderParameters = new EncoderParameters(1);
            myEncoderParameter = new EncoderParameter(myEncoder, quality);
            myEncoderParameters.Param[0] = myEncoderParameter;
            b.Save(name, myImageCodecInfo, myEncoderParameters);
        }

        /// <summary>
        /// 获取位图保存文件格式信息
        /// </summary>
        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }
    }
}

原创粉丝点击