質問

What I'm After: I'm trying to create an extra 1/4 inch of white space to be appended to the TOP of the image during the scanning process.

Using the Kofax Image Controls Toolkit is it possible within one of the following events to add extra white space to the top of the image when scanning?

  • _PageStart
  • _PageEnd
  • _PageAnnotate
  • _PageDone

Most of the properties available are read only... I know I can set the scan size in the beginning to say 14 inches and when scanning an 11 inch document I will get my extra 3 inches at the bottom of the image. I want to achieve the same principle but at the top of the document and only about a quarter of an inch tall.

役に立ちましたか?

解決

So in all the research I did trying to make this possible I came to the conclusion it is in fact not possible...

The only drawbacks about this method were the speed issues after the fact. It did end up slowing down things a good bit.. Hope this helps someone else! Upvote if it helps you please. ;)

What I ended up doing is not using the _PageAnnotate event raised by the Kofax Image Controls and in turn I ended up turning off that event by setting .hDCCreate = false on the Kofax Scan control. Not creating a DC when scanning speeds up the process a little bit not bogging down the processor with all the uncompressing and compressing of the image.

Instead of using the built in Kofax Image Controls event _PageAnnotate I used the _PageDone event and from there used native c# objects/functions to achieve what I was after. Below is the code I ended up with.

if (!Imaging.AnnotateImage(sImageFileName, "This is my annotation..."))
{
    Debug.WriteLine("Scan Error!  Could not annotate the image.");
}

The code for the class I made called '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;
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top