Page 1 of 1

Generate a bitmap from a two dimension array Rate Topic: -----

#1 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Posted 18 February 2012 - 05:55 PM

Generating A Bitmap From A Boolean Array

I was thinking on a type of encription that would convet a document to an image and the convert it back, but I found three problems.

If I read bytes as boolean from my document, then most of the image wold be white, because any non zero byte would be considered as true. The other way was to use the 'bits' that form the byte, but now I have too many to construct a pixel of any color, this is the second problem. I need to create 3 colors from every byte and it would multipy my file size for 3. The third problem was that I had to fill the balance of the rectangle with white or black values, because the documents don't have a "n x n" or "n x m" array of characters, and this balance would appear as a stip on top of the image, remember that "y" grows down and this could be a clue to decipher the encoding. So I have to find another way, we will see.

On the mean time, I made a function to make the image from the array. So I have this small program that will create a "granite" bitmap at clicking on the color picker cube.


Attached Image


It creates a bitmap sized with the indexes of the array (elements(x,y)) and sets the corresponding pixel's color with the color chosen ("getPixel"), by means of ("SetPixel"). Color for "True", white for "False".

This is a one to one bitmap, but you could double the size of the bitmat and set the bitmap coloring to 2x2 pixel square with the same color.

Here is the code.


Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

Public Class Form1
    Dim Cx(100, 100) As Boolean
    Dim Span As Integer
    Dim PickedColor As Color = Color.Black

    Private Function CreateBmp(ByRef Mx(,) As Boolean, ByVal Stride As Integer) As Image
        Dim Height As Integer
        Height = Mx.Length \ Span
        Dim Patn As New Bitmap(Stride, Height)
        For g As Integer = 0 To Height - 2
            For f As Integer = 0 To Stride - 1
                If Mx(f, g) = True Then
                    Patn.SetPixel(f, g, Color.White)
                Else
                    Patn.SetPixel(f, g, PickedColor)
                End If
            Next
        Next
        Return Patn
    End Function




The values on the array are generated at random, so it gives a random image, always changing with every click:



   Public Function RandomValue(ByVal low As Integer, ByVal high As Integer) As Boolean
        Static Random1 As New Random
        If Random1.Next(low, high + 1) Mod 2 = 0 Then
            Return False
        Else
            Return True
        End If
    End Function



If you want to save the generated image, you could use it as a background for a webpage or for any control that can show graphics:

 
    Private Sub btnSaveImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveImage.Click
        Dim i As Integer
        Dim str As String = ""
        For i = 1 To 1000
            If i < 10 Then str = "Picture_00" & i.ToString & ".jpg"
            If i > 9 And i < 100 Then str = "Picture_0" & i.ToString & ".jpg"
            If i > 99 Then str = "Picture_" & i.ToString & ".jpg"
            If i > 900 Then MsgBox("You Have Over 900 Pictures, Please Check And Delete Unnecesary Ones")
            If Not System.IO.File.Exists(str) Then
                Try
                    pbResult.Image.Save(str, System.Drawing.Imaging.ImageFormat.Jpeg)

                Catch Ex As Exception
                    MsgBox("Could Not Write To Location")
                End Try
                Exit For
            End If
        Next

    End Sub



The color can come by any means that you like, I did chose a color picker image. When you click on it, the color under the pointer is saved on "PickedColor", a color variable set at the beginning of the program.


    Private Sub pbColor_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbColor.MouseClick
        Dim bmp As New Bitmap(pbColor.Image)
        PickedColor = bmp.GetPixel(e.X, e.Y)
        For m As Integer = 0 To N1.Value - 1
            For n As Integer = 0 To N1.Value - 1
                Cx(m, n) = RandomValue(0, 1) 'For True Or False Values
            Next
        Next
        pbResult.Image = CreateBmp(Cx, Span)

    End Sub



If we want to change te size of the array and corresponding image size you would use the numeric up-down "N1", this will 'redim' the array and change the counters for the loops to set the pixels. The second picturebox is set to "Autosize", so it will change dimensions to adjust itself to the bitmap.

    Private Sub N1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles N1.ValueChanged
        ReDim Cx(N1.Value, N1.Value)
        Span = N1.Value + 1
    End Sub



That simple. If you want something more complex, check some of my other contributions.

There is an attached project.

cheers,
ricardosms.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Page 1 of 1