public static bool SaveBitmapToFile(Bitmap bitmap, string filename, EnumImageFormat format, bool cropImage) { if (bitmap != null && !string.IsNullOrEmpty(filename)) { if (cropImage) { DlgCropImage dlg = new DlgCropImage(); dlg.LoadData(bitmap); dlg.ShowDialog(); bitmap = dlg.Result as Bitmap; } if (bitmap != null) { if (System.IO.File.Exists(filename)) { System.IO.File.Delete(filename); } try { using (Bitmap bm = new Bitmap(bitmap)) { bm.Save(filename, VPLUtil.GetImageFormat(format)); } return(true); } catch (Exception err) { MessageBox.Show(string.Format(CultureInfo.InvariantCulture, "Error saving image to [{0}]. Verify it is a valid file name. {1}", filename, err.Message)); } } } return(false); }
/// <summary> /// Attempts to save int array of pixel data with the given width and height of the image at the specified path. /// Format type is used to get the type of extension to be used. /// </summary> /// <param name="pixels"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="path"></param> /// <param name="formatType"></param> public void SaveImage(int[] pixels, int width, int height, String path, EnumImageFormat formatType) { if (pixels.Length / height != width) { Console.WriteLine("Error! Data array does not match width and height!"); return; } try { Console.Write("Creating image object ... "); Bitmap image = new Bitmap(width, height, PixelFormat.Format8bppIndexed); Console.Write("Image object created successfully!" + Environment.NewLine); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { image.SetPixel(x, y, Color.FromArgb(pixels[x + y * width])); } } Console.Write("Attempting to save image to file at path" + path + " ... "); image.Save(path, GetFormat(formatType)); Console.Write("Image saved successfully!" + Environment.NewLine); } catch (IOException ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine("Error writing image to file!"); } }
/// <summary> /// Gets the format to be used in saving a given image from the type to be used and returns the object of desire in its set format. /// </summary> /// <param name="type"></param> /// <returns></returns> private ImageFormat GetFormat(EnumImageFormat type) { if (type == EnumImageFormat.png) return ImageFormat.Png; else if (type == EnumImageFormat.jpeg) return ImageFormat.Jpeg; else if (type == EnumImageFormat.gif) return ImageFormat.Gif; else if (type == EnumImageFormat.bmp) return ImageFormat.Bmp; else if (type == EnumImageFormat.tiff) return ImageFormat.Tiff; else return null; }
public bool SaveScreenImageToFile(string filename, EnumImageFormat format, bool cropImage) { return(SaveBitmapToFile(CaptureScreen(), filename, format, cropImage)); }
public bool SaveWindowUnderMouseImageToFile(string filename, EnumImageFormat format, bool cropImage) { return(SaveBitmapToFile(CaptureWindowUnderMouse(), filename, format, cropImage)); }
public bool SaveControlImageToFile(Control control, string filename, EnumImageFormat format, bool cropImage) { return(SaveBitmapToFile(CaptureWindowImage(control.Handle), filename, format, cropImage)); }
public bool SaveWindowImageToFile(string windowTitle, string filename, EnumImageFormat format, bool cropImage) { return(SaveBitmapToFile(CaptureWindowImage(windowTitle), filename, format, cropImage)); }