Пример #1
0
        private static void ExportGraphic(Control control, string filename, int width, int height, bool showDialog)
        {
            if (showDialog)
            {
                filename = ShowDialog(filename);
            }
            if (filename == null)
            {
                return;
            }
            Stream      stream = null;
            IGraphics   graphics;
            ImageFormat imageFormat = null;
            string      extension   = System.IO.Path.GetExtension(filename);

            if (!string.IsNullOrEmpty(extension))
            {
                extension = extension.ToLower();
            }
            switch (extension)
            {
            case ".svg":
                if (File.Exists(filename))
                {
                    try{
                        File.Delete(filename);
                    } catch (Exception) {
                        MessageBox.Show("The file " + filename + " is open in another program.");
                        return;
                    }
                }
                stream   = new FileStream(filename, FileMode.CreateNew);
                graphics = new SvgGraphics2(stream);
                break;

            case ".pdf":
                if (File.Exists(filename))
                {
                    try{
                        File.Delete(filename);
                    } catch (Exception) {
                        MessageBox.Show("The file " + filename + " is open in another program.");
                        return;
                    }
                }
                stream   = new FileStream(filename, FileMode.CreateNew);
                graphics = new PdfGraphics(stream, width, height);
                break;

            case ".png":
                imageFormat = ImageFormat.Png;
                Graphics g = control.CreateGraphics();
                g.Clip   = new Region(new RectangleF(0, 0, width, height));
                graphics = new CGraphics(g);
                break;

            case ".jpg":
                imageFormat = ImageFormat.Jpeg;
                Graphics g2 = control.CreateGraphics();
                g2.Clip  = new Region(new RectangleF(0, 0, width, height));
                graphics = new CGraphics(g2);
                break;

            default:
                MessageBox.Show("Could not find specified fileformat " + extension);
                return;
            }
            control.CreateControl();
            if (graphics is CGraphics)
            {
                CGraphics g      = (CGraphics)graphics;
                Bitmap    bitmap = new Bitmap(width, height, g.Graphics);
                control.DrawToBitmap(bitmap, new Rectangle(0, 0, width, height));
                bitmap.Save(filename, imageFormat);
                bitmap.Dispose();
                graphics.Dispose();
            }
            else if (control is BasicControl)
            {
                ((BasicControl)control).view.Print(graphics, control.Width, control.Height);
            }
            else
            {
                DoPaintBackground(graphics, control);
                DoPaint(graphics, control);
            }
            if (stream != null)
            {
                graphics.Dispose();
                stream.Close();
                stream.Dispose();
            }
        }
Пример #2
0
 private static void ExportGraphic(Control control, string filename, int width, int height, bool showDialog)
 {
     if (showDialog){
         filename = ShowDialog(filename);
     }
     if (filename == null){
         return;
     }
     Stream stream = null;
     IGraphics graphics;
     ImageFormat imageFormat = null;
     string extension = System.IO.Path.GetExtension(filename);
     if (!string.IsNullOrEmpty(extension)){
         extension = extension.ToLower();
     }
     switch (extension){
         case ".svg":
             if (File.Exists(filename)){
                 try{
                     File.Delete(filename);
                 } catch (Exception){
                     MessageBox.Show("The file " + filename + " is open in another program.");
                     return;
                 }
             }
             stream = new FileStream(filename, FileMode.CreateNew);
             graphics = new SvgGraphics(stream, width, height);
             break;
         case ".pdf":
             if (File.Exists(filename)){
                 try{
                     File.Delete(filename);
                 } catch (Exception){
                     MessageBox.Show("The file " + filename + " is open in another program.");
                     return;
                 }
             }
             stream = new FileStream(filename, FileMode.CreateNew);
             graphics = new PdfGraphics(stream, width, height);
             break;
         case ".png":
             imageFormat = ImageFormat.Png;
             Graphics g = control.CreateGraphics();
             g.Clip = new Region(new RectangleF(0, 0, width, height));
             graphics = new CGraphics(g);
             break;
         case ".jpg":
             imageFormat = ImageFormat.Jpeg;
             Graphics g2 = control.CreateGraphics();
             g2.Clip = new Region(new RectangleF(0, 0, width, height));
             graphics = new CGraphics(g2);
             break;
         default:
             MessageBox.Show("Could not find specified fileformat " + extension);
             return;
     }
     control.CreateControl();
     if (graphics is CGraphics){
         CGraphics g = (CGraphics) graphics;
         Bitmap bitmap = new Bitmap(width, height, g.Graphics);
         control.DrawToBitmap(bitmap, new Rectangle(0, 0, width, height));
         bitmap.Save(filename, imageFormat);
         bitmap.Dispose();
         graphics.Dispose();
     } else if (control is BasicControl){
         ((BasicControl) control).view.Print(graphics, control.Width, control.Height);
     } else{
         DoPaintBackground(graphics, control);
         DoPaint(graphics, control);
     }
     if (stream != null){
         graphics.Dispose();
         stream.Close();
         stream.Dispose();
     }
 }