Using .Net and GDI+ to pre-slug scan sheets
Here is a sample that I put together (I actually use it) to demonstrate how one can generate an image, which has squares in places for pencil marks. Scanner picks up laser ink, so printing this image over a questionnaire saves people from having to bubble stuff in themselves (and makes the data input more accurate).
The code below is the button click event together with some constants defined to ease management. The only other undefined item should be the ID, and that is the textbox where a user can provide data. The principle should be clear though.
This sample imitates the pattern of a Social Security Number.
using System.Drawing.Imaging;
public const int SLUGH2 = 15; //height
public const int SLUGW2 = 15; //width
public const int SLUGOFFSET_V2 = 4; //space between slugs, vertical
public const int SLUGOFFSET_H2 = 4; //space between slugs, horizontal
public const int SLUGOFFSET_34 = 7; //offset between 3rd and 4th slugs
public const int SLUGOFFSET_56 = 7; //offset between 5th and 6th slugs
public const int SLUGAREAW2 = 200; //width of the plot
public const int SLUGAREAH2 = 200; //height of the plot
ColorPalette cp2;
private void button324_Click(object sender, EventArgs e)
{
SolidBrush brush2 = new SolidBrush(Color.Black);
Bitmap idArea2 = new Bitmap(SLUGAREAW2, SLUGAREAH2);
Graphics g2 = Graphics.FromImage(idArea2);
g2.FillRectangle(new SolidBrush(Color.White), 0, 0, SLUGAREAW2, SLUGAREAH2);
cp2 = idArea2.Palette;
char[] arr2 = ID.Text.ToCharArray();
for (int i = 0; i < 3; i++)
{
int no = Convert.ToInt16(arr2[i].ToString());
g2.FillRectangle(brush2, i * SLUGW2 +i * SLUGOFFSET_H2,
no * SLUGH2 + no * SLUGOFFSET_V2, SLUGW2, SLUGH2);
}
for (int i = 3; i < 5; i++)
{
int no = Convert.ToInt16(arr2[i].ToString());
g2.FillRectangle(brush2, i * SLUGW2 +(i+1) * SLUGOFFSET_H2 + SLUGOFFSET_34,
no * SLUGH2 + no * SLUGOFFSET_V2, SLUGW2, SLUGH2);
}
for (int i = 5; i < 9; i++)
{
int no = Convert.ToInt16(arr2[i].ToString());
g2.FillRectangle(brush2,
i * SLUGW2 + (i+2) * SLUGOFFSET_H2 + SLUGOFFSET_34 + SLUGOFFSET_56,
no * SLUGH2 + no * SLUGOFFSET_V2, SLUGW2, SLUGH2);
}
g2.Save();
idArea2.Save(ID.Text + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
pb1.Image = idArea2;
}
The result is a jpeg image.
The code below is the button click event together with some constants defined to ease management. The only other undefined item should be the ID, and that is the textbox where a user can provide data. The principle should be clear though.
This sample imitates the pattern of a Social Security Number.
using System.Drawing.Imaging;
public const int SLUGH2 = 15; //height
public const int SLUGW2 = 15; //width
public const int SLUGOFFSET_V2 = 4; //space between slugs, vertical
public const int SLUGOFFSET_H2 = 4; //space between slugs, horizontal
public const int SLUGOFFSET_34 = 7; //offset between 3rd and 4th slugs
public const int SLUGOFFSET_56 = 7; //offset between 5th and 6th slugs
public const int SLUGAREAW2 = 200; //width of the plot
public const int SLUGAREAH2 = 200; //height of the plot
ColorPalette cp2;
private void button324_Click(object sender, EventArgs e)
{
SolidBrush brush2 = new SolidBrush(Color.Black);
Bitmap idArea2 = new Bitmap(SLUGAREAW2, SLUGAREAH2);
Graphics g2 = Graphics.FromImage(idArea2);
g2.FillRectangle(new SolidBrush(Color.White), 0, 0, SLUGAREAW2, SLUGAREAH2);
cp2 = idArea2.Palette;
char[] arr2 = ID.Text.ToCharArray();
for (int i = 0; i < 3; i++)
{
int no = Convert.ToInt16(arr2[i].ToString());
g2.FillRectangle(brush2, i * SLUGW2 +i * SLUGOFFSET_H2,
no * SLUGH2 + no * SLUGOFFSET_V2, SLUGW2, SLUGH2);
}
for (int i = 3; i < 5; i++)
{
int no = Convert.ToInt16(arr2[i].ToString());
g2.FillRectangle(brush2, i * SLUGW2 +(i+1) * SLUGOFFSET_H2 + SLUGOFFSET_34,
no * SLUGH2 + no * SLUGOFFSET_V2, SLUGW2, SLUGH2);
}
for (int i = 5; i < 9; i++)
{
int no = Convert.ToInt16(arr2[i].ToString());
g2.FillRectangle(brush2,
i * SLUGW2 + (i+2) * SLUGOFFSET_H2 + SLUGOFFSET_34 + SLUGOFFSET_56,
no * SLUGH2 + no * SLUGOFFSET_V2, SLUGW2, SLUGH2);
}
g2.Save();
idArea2.Save(ID.Text + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
pb1.Image = idArea2;
}
The result is a jpeg image.
Labels: .net c# programming gdi+
