我正在创建一个新类来操作图像,但我似乎无法从中获得我想要的速度。 我觉得打电话
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;
}
我认为你的缓慢很大一部分与你有效地绘制图像两次有关:
Bitmap annotatedImage = new Bitmap(
imageCached,
new Size(
imageCached.Width,
imageCached.Height + headerHeight
)
);
然后在:
g.DrawImage(imageCached, new Point(0, headerHeight));
我怀疑你是否将前者更改为:
Bitmap annotatedImage = new Bitmap(imageCached.Width, imageCached.Height + headerHeight);
代码的运行速度将大大加快。