Form每次paint时会DrawImage画一个外部图像,同时会画一些矢量图,如矩形,线条等.如何将这些在Form上显示的图形一起输出到一个外部图像文件(如jpg)中?

解决方案 »

  1.   


    #Region "打开和保存方法"    ''' <summary>
        ''' 从文件打开图像并更新窗体。
        ''' </summary>
        Private Sub OpenImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenImage.Click        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                ' 打开图像
                originalImage = Image.FromFile(OpenFileDialog1.FileName)
                currentImage = CType(originalImage.Clone(), Image)            ' 确定初始图像视图的适当缩放倍数。
                If currentImage.Width / 2 > MainImage.Width Or _
                 currentImage.Height / 2 > MainImage.Height Then
                    Zoom(0.25)
                ElseIf currentImage.Width > MainImage.Width Or _
                 currentImage.Height > MainImage.Height Then
                    Zoom(0.5)
                ElseIf currentImage.Width * 2 < MainImage.Width _
                 And currentImage.Height * 2 < MainImage.Height Then
                    Zoom(2)
                ElseIf currentImage.Width * 2 > MainImage.Width _
                 And currentImage.Height * 2 > MainImage.Height Then
                    Zoom(2)
                ElseIf currentImage.Width * 1.5 < MainImage.Width _
                 And currentImage.Height * 2 < MainImage.Height Then
                    Zoom(1.5)
                Else
                    Zoom(1)
                End If            ' 针对新的图像更新窗体。
                UpdateWidthandHeight()
                Undo.Enabled = False
                ImageToolStripMenuItem.Visible = True
                EditMenu.Visible = True
                SaveThumbnailAs.Enabled = True
                SaveImageAs.Enabled = True
                Resizing.Enabled = True
                Cropping.Enabled = True
                ImageInfo.Enabled = True
                MainImage.Enabled = True        End If    End Sub    ''' <summary>
        ''' 将图像保存到所选的文件。
        ''' </summary>
        Private Sub SaveImageAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveImageAs.Click
            If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                currentImage.Save(SaveFileDialog1.FileName, GetImageFormat())
            End If
        End Sub    ''' <summary>
        ''' 将当前图像保存为缩略图
        ''' </summary>
        Private Sub SaveThumbnailAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveThumbnailAs.Click        Dim sourceBitmap As New Bitmap(currentImage)
            Dim destBitmap As New Bitmap( _
             CInt(sourceBitmap.Width * thumbnailFactor), _
              CInt(sourceBitmap.Height * thumbnailFactor))        Dim destGraphic As Graphics = Graphics.FromImage(destBitmap)        destGraphic.DrawImage(sourceBitmap, 0, 0, _
             destBitmap.Width + 1, destBitmap.Height + 1)        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                destBitmap.Save(SaveFileDialog1.FileName, GetImageFormat())
            End If    End Sub    ''' <summary>
        ''' 从保存对话框获取图像格式。
        ''' </summary>
        Private Function GetImageFormat() As ImageFormat
            Select Case SaveFileDialog1.FilterIndex
                Case 1
                    Return ImageFormat.Bmp
                Case 2
                    Return ImageFormat.Jpeg
                Case 3
                    Return ImageFormat.Gif
                Case Else
                    Return ImageFormat.Tiff
            End Select
        End Function    Private Sub ExitApplication_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitApplication.Click
            Me.Close()
        End Sub#End Region