When the webform1.aspx page loads in the browser, part of its loading is to request each external image file. The image control makes a request to the barcode.aspx page and its Page_Load event handler is executed. I am not using the width and height variables much, just to get the image wide enough for the barcodes I’m using; but you could make them work proportionally, or you could use a transform to cause scaling of your barcodes as a feature addition. After obtaining the parameter values, I create an instance of the Bitmap type by specifying a width and height. The bitmap is what will be sent back to the requesting image control for display. Then I create an instance named graph that is of type Graphics using the bitmap.
I then print the proper barcode and, if text query string value is 1, I print the text underneath it. (Notice that with UPC-A it is standard to print the text of the code overlapping the bottom of the barcode graphic, so I used two filled rectangles to do that and it helped me to color them lawngreen at first to get them lined up with the textcode. These rectangles are necessary to block the graph where the textcode will print.) Lastly, since the request is from the image control, not the whole webform1.aspx page, I save the bitmap to the Response.OutputStream in the proper GIF format.
Code Listing 1 – Page Load Event
Try
_code = Request.QueryString.Item("code")
_height = CType(Request.QueryString.Item("height"), Integer)
_mode = Request.QueryString.Item("mode").ToLower
_text = CType(Request.QueryString.Item("text"), Integer)
_height = _height * 2 : _width = 105
If Not _text = 0 And Not _text = 1 Then
Throw New Exception("_text is out of range = 0-1")
End If
Dim bcBitMap As Bitmap
If _mode.ToLower = "upc-a" Then
bcBitMap = New Bitmap(_width, _height)
Else
bcBitMap = New Bitmap(_width * 2 + 20, _height + 10)
End If
Dim graph As Graphics = Graphics.FromImage(bcBitMap)
graph.Clear(Color.White)
Me.PrintBarCode(graph, _mode)
If _text = 1 Then
If _mode.ToLower = "upc-a" Then
Me.PrintUpcText(graph, _code)
Else
Me.PrintC39Text(graph, "*" & _code.Replace("*", "") & "*")
End If
End If
Try
Response.ContentType = "image/gif"
bcBitMap.Save(Response.OutputStream, ImageFormat.Gif)
Catch ex As Exception
Dim errorMsg As String = ex.message
End Try
Catch ex As Exception
Dim errorMsg As String = ex.Message
End Try