Save InkPicture image with Ink in common image formats

There are two options when you have to save an image with Ink
a) Either save the image by manually serializing it and then adding it to the Ink object using Ink.ExtendedProperties.Add(...) . By doing this, you can't send the file to anyone not able to read the file.
b) Output a file using common image formats (i.e. jpg,gif, ... )

For more information about the first method, read Saving and Loading Ink on InkPicture Control in C# or the classic book Building Tablet PC Applications.

For using the second method, you need to play with GDI+ objects. For future reference and for those who need to implement this functionality, here is the code snippet.

I assume that you have a global “Bitmap” object, which holds the InkPicture's image ( or you can get it anyway )

        private void SavePicture()
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = saveFileDialog1.FileName;
                ImageFormat imgFmt = GetImageFormat(filename);

                // create graphics object from the bitmap
                Bitmap b2 = new Bitmap(deskImage);
                Graphics g1 = Graphics.FromImage(b2);

                // make a renderer to draw ink on the graphics surface
                Renderer r = new Renderer();
                r.Draw(g1, inkPicture1.Ink.Strokes);              
               
                b2.Save(filename, imgFmt);
            }
        }

The GetImageFormat( ) method

        private ImageFormat GetImageFormat(string filename)
        {
            string ext = Path.GetExtension(filename);

            ImageFormat imgFmt = null;

            switch(ext)
            {
                case ".jpg":
                    imgFmt = ImageFormat.Jpeg;
                    break;
                case ".gif":
                    imgFmt = ImageFormat.Gif;
                    break;
                case ".bmp":
                    imgFmt = ImageFormat.Jpeg;
                    break;
                default:
                    imgFmt = ImageFormat.Jpeg;
                    filename += ".jpg";
                    break;
            }

            return imgFmt;
        }

Update: It may not be the only method so if you know any other way to do this, please leave your comments. Thanks.

No Comments