天天看點

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