有一個場景,我想合併兩張圖片。
第一張在上,第二張在下。新圖片的高等於兩張圖片高的和,寬等於兩張圖片中最寬的寬度。
最笨的方法是新建一張圖片然後循環賦值。但是速度太慢效率太低。
因此我想用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