System.Drawing.Bitmap.Save C# (CSharp) Method

Save() public method

public Save ( string path, ImageFormat format ) : void
path string
format ImageFormat
return void
        public void Save(string path, ImageFormat format)
        {
            if (path == null)
                throw new ArgumentNullException ("path");

            if (NativeCGImage == null)
                throw new ObjectDisposedException ("cgimage");

            // With MonoTouch we can use UTType from CoreMobileServices but since
            // MonoMac does not have that yet (or at least can not find it) I will
            // use the string version of those for now.  I did not want to add another
            // #if #else in here.

            // for now we will just default this to png
            var typeIdentifier = "public.png";

            // Get the correct type identifier
            if (format == ImageFormat.Bmp)
                typeIdentifier = "com.microsoft.bmp";
            //			else if (format == ImageFormat.Emf)
            //				typeIdentifier = "image/emf";
            //			else if (format == ImageFormat.Exif)
            //				typeIdentifier = "image/exif";
            else if (format == ImageFormat.Gif)
                typeIdentifier = "com.compuserve.gif";
            else if (format == ImageFormat.Icon)
                typeIdentifier = "com.microsoft.ico";
            else if (format == ImageFormat.Jpeg)
                typeIdentifier = "public.jpeg";
            else if (format == ImageFormat.Png)
                typeIdentifier = "public.png";
            else if (format == ImageFormat.Tiff)
                typeIdentifier = "public.tiff";
            else if (format == ImageFormat.Wmf)
                typeIdentifier = "com.adobe.pdf";

            // Not sure what this is yet
            else if (format == ImageFormat.MemoryBmp)
                throw new NotImplementedException("ImageFormat.MemoryBmp not supported");

            // Obtain a URL file path to be passed
            NSUrl url = NSUrl.FromFilename(path);

            // * NOTE * we only support one image for right now.

            // Create an image destination that saves into the path that is passed in
            CGImageDestination dest = CGImageDestination.Create (url, typeIdentifier, frameCount);

            // Add an image to the destination
            dest.AddImage(NativeCGImage, (NSDictionary)null);

            // Finish the export
            bool success = dest.Close ();
            //                        if (success == false)
            //                                Console.WriteLine("did not work");
            //                        else
            //                                Console.WriteLine("did work: " + path);
            dest.Dispose();
            dest = null;
        }

Same methods

Bitmap::Save ( Stream stream, ImageFormat format ) : void
Bitmap::Save ( string path ) : void

Usage Example

        private void RegisterCommands(ImagePresentationViewModel viewModel)
        {
            viewModel.SaveCommand = UICommand.Regular(() =>
            {
                var dialog = new SaveFileDialog
                {
                    Filter = "PNG Image|*.png|Bitmap Image|*.bmp",
                    InitialDirectory = Settings.Instance.DefaultPath
                };
                var dialogResult = dialog.ShowDialog();
                if (dialogResult.HasValue && dialogResult.Value)
                {
                    var tmp = viewModel.Image;
                    using (var bmp = new Bitmap(tmp))
                    {
                        if (File.Exists(dialog.FileName))
                        {
                            File.Delete(dialog.FileName);
                        }

                        switch (dialog.FilterIndex)
                        {
                            case 0:
                                bmp.Save(dialog.FileName, ImageFormat.Png);
                                break;
                            case 1:
                                bmp.Save(dialog.FileName, ImageFormat.Bmp);
                                break;
                        }
                    }
                }
            });
        }
All Usage Examples Of System.Drawing.Bitmap::Save