Advertisement
Guest User

Untitled

a guest
Jun 1st, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. // this is how the graphical object of the pictureBox1 control is declared
  2.  
  3. public static Graphics g;
  4.  
  5. private void Form1_Load(object sender, EventArgs e)
  6. {
  7.  
  8.     g = pictureBox1.CreateGraphics();
  9.  
  10. }
  11.  
  12. // this is how I save what was drawn on g
  13.  
  14. private Bitmap DrawControlToBitmap(Control control)
  15.  
  16. {
  17.  
  18.     Bitmap bitmap = new Bitmap(control.Width, control.Height);
  19.  
  20.     Graphics graphics = Graphics.FromImage(bitmap);
  21.  
  22.     Rectangle rect = control.RectangleToScreen(control.ClientRectangle);
  23.  
  24.     graphics.CopyFromScreen(rect.Location, Point.Empty, control.Size);
  25.  
  26.     return bitmap;
  27.  
  28. }
  29.  
  30. private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
  31.  
  32. {
  33.  
  34.     Bitmap bitmap = DrawControlToBitmap(pictureBox1);
  35.  
  36.     SaveFileDialog dialog = new SaveFileDialog();
  37.  
  38.     dialog.Filter = "PNG File (*.png)|*.png|JPEG File (*.jpeg)|*.jpeg|ICON File (*.ico)|*.ico|BITMAP File (*.bmp)|*.bmp|ALL FILES (*.*)|*.*";
  39.  
  40.     if (dialog.ShowDialog() == DialogResult.OK)
  41.     {
  42.  
  43.         if (dialog.FilterIndex == 1)
  44.         {
  45.             bitmap.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Png);
  46.         }
  47.  
  48.         else if (dialog.FilterIndex == 2)
  49.         {
  50.             bitmap.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
  51.         }
  52.  
  53.         else if (dialog.FilterIndex == 3)
  54.         {
  55.             bitmap.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Icon);
  56.         }
  57.  
  58.         else if (dialog.FilterIndex == 4)
  59.         {
  60.             bitmap.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
  61.         }
  62.  
  63.         else if (dialog.FilterIndex == 5)
  64.         {
  65.             bitmap.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
  66.         }
  67.  
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement