Print selection

Discuss and ask questions about CAD .NET library.

Moderators: admin, support, SDS

Post Reply
Pascal
Posts: 6
Joined: 26 Oct 2018, 14:33

Print selection

Post by Pascal » 14 Jan 2019, 17:46

Hello, is there a way to print or export a selection of the CAD image?
I’m looking for something that you can select a part of the image, just like selecting a part to zoom on that part, and then print or export that visible part?
Of maybe crop that part?

And is there an example in the demo files?

Thanks,
Pascal

support
Posts: 2834
Joined: 30 Mar 2005, 11:36
Contact:

Re: Print selection

Post by support » 14 Jan 2019, 21:25

Hello Pascal,

You can save a part of the CAD image to a MemoryStream using the following signature of an overloaded method CADImage.SaveToStream:

Code: Select all

public virtual void SaveToStream(  
   Stream str,  
   ImageFormat ImgFormat,  
   DRect aCurRect,  
   Rectangle clipRect  
) 
ImgFormat parameter specifies the file format of the saved image (Bmp, Jpeg, etc.). CurRect parameter denotes the part of the CAD image that is currently displayed on screen, while clipRect determines the part which will be saved to the stream (Stream str).

Once you have the cropped part in a memory stream, you can create a new Bitmap from it:

Code: Select all

MemoryStream ms = new MemoryStream();
...

Bitmap bmp = new Bitmap(ms);
and then draw this bitmap on a printer page by using a PrintDocument class:

Code: Select all

public static void PrintBitmap(Bitmap bitmap, string printerName, int paperWidth, int paperHeight)
{
    PrintDocument pd = new PrintDocument();
    pd.PrinterSettings.PrinterName = printerName;
    pd.PrinterSettings.DefaultPageSettings.Landscape = true;
    pd.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Custom size", paperWidth, paperHeight);
    pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
    pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

    pd.PrintPage += (sender, args) =>
    {
        Rectangle m = args.MarginBounds;
        if ((double)bitmap.Width / (double)bitmap.Height > (double)m.Width / (double)m.Height)
        {
            m.Height = (int)((double)bitmap.Height / (double)bitmap.Width * (double)m.Width);
        }
        else
        {
            m.Width = (int)((double)bitmap.Width / (double)bitmap.Height * (double)m.Height);
        }
        args.Graphics.DrawImage(bitmap, m);
    };
    pd.Print();    
}
To select a part of the CAD image with a mouse, you may use a CADEditorControl.ClipRectangle tool, as shown in the code example below.

Code: Select all

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using CADImport;
using CADImport.FaceModule;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        cadEditorControl1.EditorCADPictureBox.MouseDown += EditorCADPictureBox_MouseDown;
        cadEditorControl1.EditorCADPictureBox.MouseUp += EditorCADPictureBox_MouseUp;
    }

    void EditorCADPictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (cadEditorControl1.ClipRectangle.Enabled)
        {
            MemoryStream ms = new MemoryStream();
            DRect curRect = new DRect(cadEditorControl1.ImageRectangleF.Left, cadEditorControl1.ImageRectangleF.Top, 0, cadEditorControl1.ImageRectangleF.Right, cadEditorControl1.ImageRectangleF.Bottom, 0);
            cadEditorControl1.Image.SaveToStream(ms, ImageFormat.Bmp, curRect, cadEditorControl1.ClipRectangle.ClientRectangle);
            Bitmap bmp = new Bitmap(ms);
            PrintBitmap(bmp, "Microsoft Print to PDF", 297, 210);

            cadEditorControl1.ClipRectangle.DisableRect();
            cadEditorControl1.Image.SelectionMode = SelectionEntityMode.Enabled;
        }
    }

    void EditorCADPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        cadEditorControl1.ClipRectangle.EnableRect(RectangleType.Zooming);
        cadEditorControl1.Image.SelectionMode = SelectionEntityMode.Disabled;
    }

}
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply