我正在创建一个新的类来处理图像,但似乎无法达到我想要的速度。我觉得调用g.DrawImage()
所花的时间比应有的时间要长。
当使用 LeadTools SDK 在项目中设置相同的流程时,我使用类似于字节复制的方法来复制每一行像素。这样可以更快地获得结果。
注意:我有一个简单的 LogDebug() 函数(我知道它很可靠),它可以跟踪滴答声以进行一些基准测试。我不想关注这一点,而是关注处理位图数据和速度的不同方法。
测试结果
当设置headerHeight = 0;
(不在原始图像顶部添加额外的空白)时,我得到以下结果。
/*
Creating text on image pages.
Added text to image 1. (920ms)
Added text to image 2. (303ms)
Added text to image 3. (368ms)
Saved new image. (307ms)
Total time to generate text on image: 00h:00m:01s:908ms.
*/
当设置headerHeight = 100;
(在原始图像顶部添加额外的空白)时,我得到以下结果。
/*
Creating text on image pages.
Added text to image 1. (2394ms)
Added text to image 2. (438ms)
Added text to image 3. (441ms)
Saved new image. (314ms)
Total time to generate stamp: 00h:00m:03s:598ms.
*/
来源
private void TestCreateTextOnImage()
{
LogDebug("Creating text on image pages.", false);
for (int p = 1; p <= 3; p++)
{
AddTextToImage(p);
LogDebug($"Added text to image {p}.");
}
SaveImage();
LogDebug("Saved new image.");
LogDebug("Total time to generate text on image: [TotalTicks].");
}
// Test adding text to an image
List<Image> _imageCachedPages = new List<Image>();
public void AddTextToImage(int pageNum)
{
string fontName = "Verdana";
int fontSize = 60;
// Cache the 3 page image
Image imageCached = Image.FromFile(@"c:\test\3_page_image.tif");
// Select page 2 of the three page image
imageCached.SelectActiveFrame(FrameDimension.Page, pageNum - 1);
// Test add text
int headerHeight = 0;
// Also test adding extra white space (expand image canvas)
headerHeight = 100;
// Create new image that will be the bottom layer (blank canvas)
Bitmap annotatedImage = new Bitmap(
imageCached,
new Size(
imageCached.Width,
imageCached.Height + headerHeight
)
);
annotatedImage.SetResolution(imageCached.HorizontalResolution, imageCached.VerticalResolution);
using (Graphics g = Graphics.FromImage(annotatedImage))
{
// Paint canvas white
Rectangle r = new Rectangle(0, 0, imageCached.Width, imageCached.Height);
g.FillRectangle(Brushes.White, r);
// Paint original image on top of new canvas
g.DrawImage(imageCached, new Point(0, headerHeight));
// The code I used with the LeadTools SDK that seems much faster
/*
byte[] buffer = new byte[srcImage.BytesPerLine];
for (int yH = 0; yH < srcImage.Height; yH++)
{
srcImage.GetRow(yH, buffer, 0, buffer.Length);
annotatedImage.SetRow(yH + headerHeight, buffer, 0, buffer.Length);
}
*/
// Create the font objects
FontFamily fontFamily = new FontFamily(fontName);
Font font = new Font(fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
// Create a rect to define the position and size of the annotated text
RectangleF positionRect = new RectangleF(20, 20, 500, 500);
// Draw column text
g.DrawString("Sample Text\nLine 2", font, Brushes.Black, positionRect);
}
// Save the image to the new image's page collection at the correct page position
while (_imageCachedPages.Count < pageNum)
_imageCachedPages.Add(null);
_imageCachedPages[pageNum - 1] = annotatedImage;
}
我认为你的速度缓慢很大程度上与你实际上绘制了两次图像有关:
然后在:
我怀疑如果你将前者改为:
代码将运行得更快。