Resize c# generated bitmap for better visualization

Asked

Viewed 532 times

0

I have a program to generate bar code of the type Interleaved 2 of 5. I got the code from this one website but as it was posted in 2007 I did not get answer to my question. The program generates a bitmap with the barcode and insert it into a picture-box. Meanwhile this bitmap is very small making it impossible for a reader to read the code. I have tried to resize the bitmap at the time of its creation but it "always" occupies the same space. The image below is how it shows the barcode. The base code was the numbers "1234567890" and the two label below the button were for testing the first is the width and the second is height of bitmap :

inserir a descrição da imagem aqui

Follow the full code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public string CalcCheckSum(string CheckNum)
    {
        int i;
        int j;
        int checkval = 0;
        j = 3;
        i = CheckNum.Length - 1;
        while (i > 0)
        {
            checkval += Convert.ToInt32(CheckNum.Substring(i, 1)) * j;
            j = j ^ 2;
            i -= 1;
        }
        checkval = (10 - (checkval % 10)) % 10;
        return checkval.ToString();
    }

    public string Get2of5Pattern(string letter)
    {
        string tmpPattern = "";
        switch (letter)
        {
            case "0":
                tmpPattern = "NNWWN";
                break;
            case "1":
                tmpPattern = "WNNNW";
                break;
            case "2":
                tmpPattern = "NWNNW";
                break;
            case "3":
                tmpPattern = "WWNNN";
                break;
            case "4":
                tmpPattern = "NNWNW";
                break;
            case "5":
                tmpPattern = "WNWNN";
                break;
            case "6":
                tmpPattern = "NWWNN";
                break;
            case "7":
                tmpPattern = "NNNWW";
                break;
            case "8":
                tmpPattern = "WNNWN";
                break;
            case "9":
                tmpPattern = "NWNWN";
                break;
        }
        return tmpPattern;
    }

    public Bitmap Print2of5Interleaved()
    {
        string Content = "1234567890";
        string CheckSum = CalcCheckSum(Content);
        string startcode = "1010";
        string stopcode = "1101";
        int startX = 0;
        int startY = 0;
        int endY = startY + 40;
        int curX;
        int sectionIndex = 0;
        int pairIndex = 0;
        int barIndex = 0;
        int spaceIndex = 0;

        Graphics g;
        Bitmap bmp = new Bitmap(10000, 8000);
        g = Graphics.FromImage(bmp);

        curX = startX;
        //Content = Content + CheckSum;
        if ((Content.Length % 2) != 0)
        {
            //odd number, fill in a leading zero
            Content = "0" + Content;
        }
        //draw the start marker
        foreach (char digit in startcode)
        {
            if (digit == '1')
            {
                g.DrawLine(Pens.Black, curX, startY, curX, endY);
                curX += 1;
            }
            else
            {
                curX += 1;
            }
        }
        //draw the content
        for (int i = 0; i < Content.Length; i += 2)
        {
            string pair = Content.Substring(i, 2);
            string barPattern = Get2of5Pattern(pair.Substring(0, 1));
            string spacePattern = Get2of5Pattern(pair.Substring(1, 1));
            barIndex = 0;
            spaceIndex = 0;
            sectionIndex = 0;
            while (sectionIndex < 10)
            {
                if ((sectionIndex % 2) == 0)
                {
                    //bar 0,2,4,6,8 positions
                    pairIndex = 0;
                    if (barPattern.Substring(barIndex, 1) == "W")
                    {
                        //draw wide bar
                        while (pairIndex < 2)
                        {
                            g.DrawLine(Pens.Black, curX + pairIndex, startY, curX + pairIndex, endY);
                            pairIndex++;
                        }
                        curX = curX + 2;
                    }
                    else
                    {
                        //draw narrow bar
                        g.DrawLine(Pens.Black, curX + pairIndex, startY, curX + pairIndex, endY);
                        curX = curX + 1;
                    }
                    barIndex++;
                }
                else
                {
                    //space 1,3,5,7,8 positions
                    if (spacePattern.Substring(spaceIndex, 1) == "W")
                    {
                        //simulate drawing a wide white space
                        curX = curX + 2;
                    }
                    else
                    {
                        //simulate drawing a narrow white space
                        curX = curX + 1;
                    }
                    spaceIndex++;
                }
                sectionIndex += 1;
            }
        }
        //draw the stop marker
       foreach (char digit in stopcode)
        {
            if (digit == '1')
            {
                g.DrawLine(Pens.Black, curX, startY, curX, endY);
                curX += 1;
            }
            else
            {
                curX += 1;
            }
        }
        return bmp;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = Print2of5Interleaved();
        label1.Text = Print2of5Interleaved().Width.ToString();
        label2.Text = Print2of5Interleaved().Height.ToString();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }


}}

And in this other image is how the creation property of the Bitmap, I put to be created from scratch as new Bitmap(width,height) with the values shown in the previous image in labels. But when I pass the mouse over it shows this definition that the bitmap is being created with the dimensions of an existing image new Bitmap(Image original, Size newSize).

inserir a descrição da imagem aqui I need an urgent help to increase this bitmap and make the reader can read and then check if the program is correct, I’m warning you I’m beginner in programming even more in C#!! Thanks in advance!

  • @ctgPi worked, thanks! Leave it as an answer so I can mark as right.

1 answer

0


You can try something like

g.ScaleTransform(2.0f, 2.0f)

The call ScaleTransform makes all the following graphic commands to be drawn with the specified size increase.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.