VB.NET合併圖片

有一個場景,我想合併兩張圖片。

第一張在上,第二張在下。新圖片的高等於兩張圖片高的和,寬等於兩張圖片中最寬的寬度。

最笨的方法是新建一張圖片然後循環賦值。但是速度太慢效率太低。

因此我想用GDI+來繪製圖像。

Public Function MergeImages(ByVal Pic1 As Image, ByVal pic2 As Image) As Image

    Dim MergedImage As Image
    Dim Wide, High As Integer
    High = Pic1.Height + pic2.Height
    If Pic1.Width >= pic2.Width Then
        Wide = Pic1.Width
    Else
        Wide = pic2.Width
    End If
    Dim bm As New Bitmap(Wide, High)
    Dim gr As Graphics = Graphics.FromImage(bm)
    Dim rect As New Rectangle(0, 0, Wide - 1, High - 1)

    gr.DrawRectangle(Pens.White, rect)
    gr.FillRectangle(Brushes.White, rect)
    gr.DrawImage(Pic1, 0, 0)
    gr.DrawImage(pic2, 0, Pic1.Height)
    MergedImage = bm
    gr.Dispose()

    Return MergedImage
End Function



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章