C#将指定picturebox图片保存到本地

来源:互联网 发布:java连接redis集群 编辑:程序博客网 时间:2024/05/16 00:36

将指定图片以某种格式保存到给定路径中


private void pictureBox1_DoubleClick(object sender, EventArgs e)

        {
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "图片保存";
            saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp";
            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = saveImageDialog.FileName.ToString();
                if (fileName != "" && fileName != null)
                {
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
                    System.Drawing.Imaging.ImageFormat imgformat = null;


                    if (fileExtName != "")
                    {
                        switch (fileExtName)
                        {
                            case "jpg":
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case "bmp":
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                            default:
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                        }


                        try
                        {
                            Bitmap bit = new Bitmap(pictureBox1.BackgroundImage);
                            MessageBox.Show(fileName);
                            pictureBox1.BackgroundImage.Save(fileName, imgformat);
                        }
                        catch
                        {


                        }
                    }
                }
            }
        }
0 0
原创粉丝点击