This project is read-only.

export layout as images

Dec 6, 2011 at 9:51 PM
Edited Dec 7, 2011 at 12:17 AM

Adding the following codes in the LayoutControl.cs, your layout can be exported as images, not only printed as .xps file.

of course, you should add a menuItem in the LayoutMenuStrip.cs to invoke the public function 

	ShowExportDialog(). 
	note: The core function of SaveLayoutAsBitmap() is based on the PrintPage().

        public void ShowExportDialog()
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();   
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.CheckFileExists = false;
            saveFileDialog1.AddExtension = true;            
            saveFileDialog1.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF-Plus (*.emf)|*.emf|EMF-Dual (*.emf)|*.emf|EMF (*.emf)|*.emf|PNG (*.png)|*.png|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif";
            saveFileDialog1.FilterIndex = 2;


            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string _fileName = saveFileDialog1.FileName;

                if (_fileName == string.Empty)
                    return;

                SaveLayoutAsBitmap(_fileName);
            }
        }

        private ImageFormat getMapFormatfromFile(string fileName)
        {
            ImageFormat format = ImageFormat.Bmp;

            if (fileName.EndsWith("bmp"))
            {
                format = ImageFormat.Bmp;
            }
            else if (fileName.EndsWith("jpg"))
            {
                format = ImageFormat.Jpeg;
            }
            else if (fileName.EndsWith("emf"))
            {
                format = ImageFormat.Emf;
            }
            else if (fileName.EndsWith("emf"))
            {
                format = ImageFormat.Emf;
            }
            else if (fileName.EndsWith("gif"))
            {
                format = ImageFormat.Gif;
            }
            else if (fileName.EndsWith("png"))
            {
                format = ImageFormat.Png;
            }
            else if (fileName.EndsWith("tif"))
            {
                format = ImageFormat.Tiff;
            }

            return format;
        }

        private void SaveLayoutAsBitmap(string fileName)
        {
            int mapWidth = _printerSettings.DefaultPageSettings.PaperSize.Width;
            int mapHeight = _printerSettings.DefaultPageSettings.PaperSize.Height;
            Bitmap myBitmap = new Bitmap(mapWidth, mapHeight);
            
            Graphics g = Graphics.FromImage((Image)myBitmap);
            g.Clear(Color.White);   
            
            LayoutElement le;
            for (int i = LayoutElements.Count - 1; i >= 0; i--)
            {
                le = LayoutElements[i];
                LayoutText lt = le as LayoutText;
                if (lt != null)
                {
                    // Text was incorrectly losing final letters.
                    RectangleF r = le.Rectangle;
                    SizeF lettersize = g.MeasureString("0", lt.Font);
                    r.Width += lettersize.Width;
                    g.Clip = new Region(r);
                }
                else
                {
                    g.Clip = new Region(le.Rectangle);
                }
                le.DrawBackground(g, true);
                le.Draw(g, true);
                le.DrawOutline(g, true);
            }

            //myBitmap.MakeTransparent(Color.White);
            myBitmap.Save(fileName, getMapFormatfromFile(fileName));
            g.Dispose();
        }
Dec 6, 2011 at 11:32 PM

Hi, could you say something more about this code?