How to save a picturebox control as a jpeg file after it's edited

前端 未结 5 1290
我在风中等你
我在风中等你 2020-12-17 20:50

I have a PictureBox on my Windows Forms application.

I load a picture in it and I have enabled the Paint event in my code. It draws a recta

5条回答
  •  悲&欢浪女
    2020-12-17 20:53

    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);
            }
        }
    

提交回复
热议问题