Print selection

Discuss and ask questions about CAD .NET library.

Moderators: admin, support, SDS

Post Reply
Pascal
Posts: 14
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: 2846
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

Pascal
Posts: 14
Joined: 26 Oct 2018, 14:33

Re: Print selection

Post by Pascal » 19 Mar 2019, 15:03

Hello, I have another question about this.
The above example works fine, but my client wants more detail, just like the full detail in SaveToFile or SaveAsDXF. There you can zoom in and get full detail in the exported file.

Therefore my question:
In the examples above, you export what’s in the selection rectangle to a MemoryStream.
Is this also possible to export (or save) this selection with SaveToFile and SaveAsDXF?

Or is there maybe a crop function that crops the CadImage to the selection?
Then the user just has to select the area he wants to export, crop it, and then I can use SaveToFile and SaveAsDXF to export what’s left I full detail.

Thanks,
Pascal

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

Re: Print selection

Post by support » 19 Mar 2019, 22:27

Hello Pascal,

It is also possible to save this selection (CADEditorControl.ClipRectangle.ClientRectangle) to a raster image using the following signature of an overloaded method CADImage.SaveToFile:

Code: Select all

public virtual void SaveToFile(  
   string FileName,  
   ImageFormat ImgFormat,  
   DRect aCurRect,  
   Rectangle clipRect  
) 
clipRect parameter determines the area which will be clipped and exported to a raster image.

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

Pascal
Posts: 14
Joined: 26 Oct 2018, 14:33

Re: Print selection

Post by Pascal » 29 Mar 2019, 10:02

Thank you for your answer. I’m going to look at this.
But to be sure: is there no way to crop, or use SaveAsDXF to get a fully detailed export in a DXF of a selected area?
A (raster) image loses detail when you zoom on it. Or am I wrong here?

This is not just for printing anymore but for a detailed export of a selected area.

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

Re: Print selection

Post by support » 29 Mar 2019, 20:58

Hello Pascal,

When it comes to the DXF export, you cannot save a selected area, you can save certain CAD entities (e.g. CADImage.SelectedEntities):
  • Create a new CADImage instance

    Code: Select all

    CADImage cadImage = new CADImage();
    cadImage.InitialNewImage();
    
  • Create copies of the entities using a constructor of CADEntity descendant classes and the method CADEntity.AssignEntity and then add the created copies to the new CADImage. Or copy/paste the entities into the new CADImage using CADImage.CopyEntities and CADImage.PasteEntities methods.
A (raster) image loses detail when you zoom on it. Or am I wrong here?
That's correct. Raster image loses details (becomes blurry and pixelated) when zooming in.

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

Pascal
Posts: 14
Joined: 26 Oct 2018, 14:33

Re: Print selection

Post by Pascal » 01 Apr 2019, 13:45

oke, thank you

Post Reply