快速的方式来创建位图阵列的大的位图? [英] fast way to create a big bitmap from an array of bitmap?

查看:232
本文介绍了快速的方式来创建位图阵列的大的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code,

复制/粘贴在一个新的winform应用程序,如果你运行它这个会写你的桌面上的文件: test123abcd.png

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

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim SquareSize = 5
    Dim GridX = 2500
    Dim GridY = 2500
    Dim SquareCount = GridX * GridY - 1
    Dim sw As New Stopwatch

    Dim Rect(4) As Rectangle
    Rect(0) = New Rectangle(0, 3, 3, 1)
    Rect(1) = New Rectangle(3, 0, 1, 3)
    Rect(2) = New Rectangle(3, 3, 3, 1)
    Rect(3) = New Rectangle(0, 0, 1, 3)

    Dim fullsw = Stopwatch.StartNew
    Using board = New Bitmap(SquareSize * (GridX + 1), SquareSize * (GridY + 1), Imaging.PixelFormat.Format32bppPArgb)
        Using graph = Graphics.FromImage(board)
            Using _board = New Bitmap(SquareSize, SquareSize, Imaging.PixelFormat.Format32bppPArgb)
                Using g As Graphics = Graphics.FromImage(_board)
                    For i = 0 To SquareCount
                        g.Clear(If((i And 1) = 1, Color.Red, Color.Blue))
                        g.FillRectangles(Brushes.White, Rect)
                        sw.Start()
                        graph.DrawImageUnscaled(_board, ((i Mod GridX) * SquareSize), ((i \ GridY) * SquareSize))
                        sw.Stop()
                    Next
                End Using
            End Using
        End Using
        fullsw.Stop()
        board.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\test123abcd.png", Imaging.ImageFormat.Png)
    End Using

    MessageBox.Show("Full SW: " & fullsw.ElapsedMilliseconds & Environment.NewLine &
                    "DrawImageUnscaled SW: " & sw.ElapsedMilliseconds)
End Sub

中所花费的时间大约40%至45%是在 DrawImageUnscaled ,约23秒我目前的电脑上,而整个事情需要约50秒。

about 40% to 45% of the time spent is on DrawImageUnscaled, about 23 seconds on my current computer while the whole thing take about 50 seconds

是有办法加快 DrawImageUnscaled ? (也许整个事情?)

is there a way to speed up DrawImageUnscaled? (and maybe the whole thing?)

修改 - 在vb.net的问题,答案在C#

EDIT - question in vb.net, answer in c#

推荐答案

哇,我喜欢当它worthed不安全code,解决了我用C#问题到底

wow I like unsafe code when it is worthed, solved my problem with c# in the end

这里是code,这是我的问题。

here is the code, which is about 70x faster to the code in my question

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace BmpFile
{
    public class BmpTest
    {
        private const int PixelSize = 4;

        public static long Test(int GridX, int GridY, int SquareSize, Rectangle[][] Rect)
        {
            Bitmap bmp = new Bitmap(GridX * SquareSize, GridY * SquareSize, PixelFormat.Format32bppArgb);
            BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                          System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                          bmp.PixelFormat);

            int Stride = bmd.Stride;
            int Height = bmd.Height;
            int Width = bmd.Width;

            int RectFirst = Rect.GetUpperBound(0);
            int RectSecond;
            int Offset1, Offset2, Offset3;
            int i, j, k, l, w, h;
            int FullRow = SquareSize * Stride;
            int FullSquare = SquareSize * PixelSize;

            var sw = System.Diagnostics.Stopwatch.StartNew();
            unsafe
            {
                byte* row = (byte*)bmd.Scan0;

                //draw all rectangles
                for (i = 0; i <= RectFirst; ++i)
                {

                    Offset1 = ((i / GridX) * FullRow) + ((i % GridX) * FullSquare) + 3;
                    RectSecond = Rect[i].GetUpperBound(0);

                    for (j = 0; j <= RectSecond; ++j)
                    {
                        Offset2 = Rect[i][j].X * PixelSize + Rect[i][j].Y * Stride;
                        w=Rect[i][j].Width;
                        h=Rect[i][j].Height;
                        for (k = 0; k <= w; ++k)
                        {
                            Offset3 = k * PixelSize;
                            for (l = 0; l <= h; ++l)
                            {
                                row[Offset1 + Offset2 + Offset3 + (l * Stride)] = 255;
                            }
                        }
                    }
                }

                //invert color
                for (int y = 0; y < Height; y++)
                {
                    Offset1 = (y * Stride) + 3;

                    for (int x = 0; x < Width; x++)
                    {
                        if (row[Offset1 + x * PixelSize] == 255)
                        {
                            row[Offset1 + x * PixelSize] = 0;
                        }
                        else
                        {
                            row[Offset1 + x * PixelSize] = 255;
                        }
                    }
                }
            }
            sw.Stop();

            bmp.UnlockBits(bmd);

            bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.png", ImageFormat.Png);
            bmp.Dispose();

            return sw.ElapsedMilliseconds;
        }
    }
}

这篇关于快速的方式来创建位图阵列的大的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆