scale the image to save

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
zpcanada
Posts: 1
Joined: 06 Oct 2007, 03:45
Location: Canada

scale the image to save

Post by zpcanada » 06 Oct 2007, 03:54

Hi, Sergey

I want to save dxf files as images (preferably bmp format) into memory stream or image files and control image size, for example, less than 300k. How can I scale the image before I save it?
Here is the code I am using now, but the image is too big.

Friend WithEvents DXFCadImport As CADImport.CADImportControlModule.CADImportControl
Dim gr As Graphics = Me.DXFCadImport.CreateGraphics()
With cadimg
.IsWithoutMargins = True
.LoadFromFile(file)
.Is3D = False
.Millimetres = False 'set default unit to inches
.SaveToStream(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg, SetRealSize(cadimg, gr))
End With

Private Function SetRealSize(ByVal img As CADImage, ByVal gr As Graphics) As CADImport.DRect
Dim realSize As CADImport.DRect = New CADImport.DRect(0, 0, 1, 1)
img.SetNewMMToPixel(gr)
Dim realScaleDouble As Double = CDbl(realSize.Width / img.AbsWidth * img.MMToPixelX / 25.4)
realSize.Width = (realSize.Width / realScaleDouble)
realSize.Height = (realSize.Height / realScaleDouble)
Dim wh As Double = CDbl(img.AbsWidth / img.AbsHeight)
Dim new_wh As Double = CDbl(realSize.Width / realSize.Height)
If (new_wh > wh) Then
realSize.Width = realSize.Height * wh
Else
If (new_wh < wh) Then
realSize.Height = realSize.Width / wh
End If
End If
Return realSize
End Function

zpcanada

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

Post by support » 08 Oct 2007, 17:32

Hello!

We recommend to create some CADImport.DRect rectangle and to put it in to <b>SaveToStream</b> instead of SetRealSize(cadimg, gr).

SetRealSize calculates real rectangle which takes an image if drawing in 1:1 scale.

When creating CADImport.DRect rectangle use ratio basing on Me.FCADImage.AbsWidth and Me.FCADImage.AbsHeight properties.

Sergey.


Please post questions to the forum or write to support@cadsofttools.com

Post Reply