How to save as picture?

时间秒杀一切 提交于 2019-12-11 16:53:21

问题


I just created a simple webpage in which there is a PictureBox inside a Panel, the PictureBox allow users to import picture, and the Panel allow the user to insert color, so how can i export / save it as .jpeg file?


回答1:


pictureBox1.Image.Save(filePath, ImageFormat.Jpeg);

Check this MSDN reference for further knowledge.




回答2:


Here is my solution with additional support to various file types:

 public void ExportToBmp(string path)
        {
            using(var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height))
            {
            pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);
            ImageFormat imageFormat = null;

            var extension = Path.GetExtension(path);
            switch (extension)
            {
                case ".bmp":
                    imageFormat = ImageFormat.Bmp;
                    break;
                case ".png":
                    imageFormat = ImageFormat.Png;
                    break;
                case ".jpeg":
                case ".jpg":
                    imageFormat = ImageFormat.Jpeg;
                    break;
                case ".gif":
                    imageFormat = ImageFormat.Gif;
                    break;
                default:
                    throw new NotSupportedException("File extension is not supported");
            }

            bitmap.Save(path, imageFormat);
            }
        }


来源:https://stackoverflow.com/questions/14392261/how-to-save-as-picture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!