天天看点

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