我有一个VB.NET程序,它在Solidworks中重新构建零件和装配体,然后使用SaveBMP()在面板中显示装配图的图像。我的问题是没有显示图像 - 只要用户在Solidworks图形中移动尺寸的位置,位图的整个位置和大小就会发生变化,我需要进入我的代码来重新调整裁剪并调整值。
将图形保存为位图图像:
swModel.SaveBMP(BMPName, 1700, 1100) '17x11 aspect ratio
裁剪图像以删除空白区域:
Function CropDrawing(ByVal OldImage As Image) As Image
CropDrawing = Nothing
Dim X As Integer = GetValue(frmImageScale.txtCropX.Text)
Dim Y As Integer = GetValue(frmImageScale.txtCropY.Text)
Dim Width As Integer = GetValue(GetValue(frmImageScale.txtCropW.Text))
Dim Height As Integer = GetValue(frmImageScale.txtCropH.Text)
Dim CropRect As New Rectangle(X, Y, Width, Height)
Try
Dim CropImage As Bitmap = New Bitmap(CropRect.Width, CropRect.Height)
Using grp = Graphics.FromImage(CropImage)
grp.DrawImage(OldImage, New Rectangle(0, 0, CropImage.Width, CropImage.Height), CropRect, GraphicsUnit.Pixel)
OldImage.Dispose()
'resave
CropImage.Save(BMPName)
End Using
Return CropImage
Catch ex As Exception
'caught if width and height = 0
End Try
End Function
调整图像大小以适合面板:
Public Function ResizeImage(ByVal OldImage As Image, ByVal TargetWidth As Integer, ByVal TargetHeight As Integer) As System.Drawing.Image
Return New Bitmap(OldImage, TargetWidth, TargetHeight)
End Function
创建位图后,我裁剪图像然后调整大小以填充面板。
对于为何发生这种情况的任何建议表示赞赏!