fast way to create a big bitmap from an array of bitmap?

后端 未结 2 516
死守一世寂寞
死守一世寂寞 2021-01-22 14:01

I have this code,

copy/paste in a new winform app and this will write a file on your desktop if you run it: test123abcd.png

Private Sub Form         


        
2条回答
  •  醉话见心
    2021-01-22 14:57

    By assuming that the generation part (g.FillRectangles(Brushes.White, Rect), pretty time-consuming too) cannot be avoided, the best thing you can do is avoiding a second graph-generation process (also for board) and just copying the information from _board. Copying is much quicker than a new generation (as shown below), but you have the problem that the source information (_board) do not match the destination format (board by relying on .SetPixel) and thus you will have to create a function determining the current pixel (X/Y point) from the provided information (current rectangle).

    Below you can see a simple code showing the time requirement differences between both approaches:

    Dim SquareSize As Integer = 5
    Dim _board As Bitmap = Bitmap.FromFile("in.png")
    Dim board As Bitmap = New Bitmap(_board.Width * SquareSize, _board.Height * SquareSize)
    
    For x As Integer = 0 To _board.Width - 1
        For y As Integer = 0 To _board.Height - 1
            board.SetPixel(x * SquareSize, y * SquareSize, _board.GetPixel(x, y))
        Next
    Next
    
    board.Save("out1.png", Imaging.ImageFormat.Png)
    
    
    board = New Bitmap(_board.Width, _board.Height)
    
    Using board
        Using graph = Graphics.FromImage(board)
            Using _board
                Using g As Graphics = Graphics.FromImage(_board)
                    For x As Integer = 0 To _board.Width - 1
                        For y As Integer = 0 To _board.Height - 1
                            graph.DrawImageUnscaled(_board, x, y)
                        Next
                    Next
                End Using
            End Using
        End Using
        board.Save("out2.png", Imaging.ImageFormat.Png)
    End Using
    

    Bear in mind that it is not a "properly-working code". Its whole point is showing how to copy pixels between bitmaps (by multiplying by a factor, just to get different outputs than inputs); and putting the DrawImageUnscaled method under equivalent conditions (although the output picture is, logically, different) to get a good feeling of the differences in time requirements between both methodologies.

    As said via comment, this is all what I can do under the current conditions. I hope that will be enough to help you find the best solution.

提交回复
热议问题