所以在我所做的所有研究中,我试图让这成为可能,我得出的结论是,这实际上是不可能的......
这种方法的唯一缺点是事后的速度问题。它确实最终减慢了速度。希望这对其他人有帮助!如果对您有帮助,请点赞。;)
我最终做的不是使用 Kofax Image Controls 引发的 _PageAnnotate 事件,而我最终通过在 Kofax Scan 控件上设置 .hDCCreate = false 来关闭该事件。扫描时不创建 DC 会稍微加快处理速度,不会因图像的所有解压缩和压缩而使处理器陷入困境。
我没有使用内置的 Kofax Image Controls 事件 _PageAnnotate,而是使用了 _PageDone 事件,并从那里使用本机 c# 对象/函数来实现我所追求的。下面是我最终得到的代码。
if (!Imaging.AnnotateImage(sImageFileName, "This is my annotation..."))
{
Debug.WriteLine("Scan Error! Could not annotate the image.");
}
我制作的课程代码“Imaging.cs”......
static class Imaging
{
public static bool AnnotateImage(string sFilePath, string sText)
{
try
{
// Get an ImageCodecInfo object that represents the TIFF codec.
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/tiff");
Encoder myEncoder = Encoder.Compression;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
//Create some global variables
Point pointPosition = new Point(0, 0);
PointF pPos = new PointF((float)pointPosition.X, (float)pointPosition.Y);
Bitmap newBmp;
Graphics g;
Font fNewFont;
SizeF sizeTextSize;
Rectangle rectTextSize;
//Set inital width and height variables
int iW;
int iH;
int iAnnotationH = 44;
using(Image iSource = Image.FromFile(sFilePath))
{
iW = iSource.Width;
iH = iSource.Height;
//Increase the height of the image
iH += iAnnotationH;
//Create the new bitmap object
newBmp = new Bitmap(iW, iH);
newBmp.SetResolution(200.0F, 200.0F);
g = Graphics.FromImage(newBmp);
g.Clear(Color.White);
g.DrawImageUnscaled(iSource, 0, iAnnotationH, iW, iH);
//Create the font object to draw the annotation text
fNewFont = new Font("Verdana", 12);
//Get the size of the area to be drawn then convert it to a rect
sizeTextSize = g.MeasureString(sText, fNewFont);
rectTextSize = new Rectangle(pointPosition.X, pointPosition.Y, (int)sizeTextSize.Width, (int)sizeTextSize.Height);
//Draw a white rect
g.FillRectangle(Brushes.White, rectTextSize);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawString(sText, fNewFont, Brushes.Black, pPos);
}
//Save the changed image
newBmp.Save(sFilePath, myImageCodecInfo, myEncoderParameters);
}
catch
{
return false;
}
return true;
}
static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
}