问题
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