Bit image

Hello,

Does somebody know what the replacement function for BitArray in microframework?

Thank you.

There is no BitArray in MF. When we work with small devices we have to think small. BitArray is more of a unneeded “luxury” in full framework where each bit is represented as a boolean. You can easily work with bits using bitwise operators.You will loose convenience of the BitArray,but it will be much effective.

What are you trying to do with it?

thanx for the fast reply,

i’m trying to send a bit image to a esc/pos printer.

 private class BitmapData
        {
            public [b]BitArray[/b] Dots
            {
                get;
                set;
            }

            public int Height
            {
                get;
                set;
            }

            public int Width
            {
                get;
                set;
            }
        }

anyone an idea for a substitue for bitarray in micro framweork?

anyone?? :frowning:

Your question is not clear. What are you trying to do? Forget details, like printer/image…etc. What are you trying to accomplish?

first i am trying to generate a monochrome image and put it in a bitarray.

this is the original code

private static BitmapData GetBitmapData(string bmpFileName)
{
    using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
    {
        var threshold = 127;
        var index = 0;
        var dimensions = bitmap.Width * bitmap.Height;
        var dots = new BitArray(dimensions);
 
        for (var y = 0; y < bitmap.Height; y++)
        {
            for (var x = 0; x < bitmap.Width; x++)
            {
                var color = bitmap.GetPixel(x, y);
                var luminance = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                dots[index] = (luminance < threshold);
                index++;
            }
        }
 
        return new BitmapData()
            {
                Dots = dots,
                Height = bitmap.Height,
                Width = bitmap.Width
            };
    }
}

i modified it to my programs need. i most say i am a student, new to microframework and c# so i beg youre pardon if my questions are not clear.

So this is the code i am trying to use to send to the printer. what i liked to know is can i use byte [] to replace BitArray?

class BitImage
    {
        public static byte[] GetBitmapBytes(this Bitmap bitmap)
        {
            const int threshold = 127;
            int index = 0;
            int dimensions = bitmap.Height * bitmap.Width;

            byte[] data = new byte[dimensions];

            for (int vertical = 0; vertical < bitmap.Height; vertical++)
                for (int horizontal = 0; horizontal < bitmap.Width; horizontal++)
                {
                    var C = bitmap.GetPixel(vertical, horizontal);
                    var R = ColorUtility.GetRValue(C);
                    var G = ColorUtility.GetGValue(C);
                    var B = ColorUtility.GetBValue(C);
                    int luminance = (int)(R * 0.3 + G * 0.59 + B * 0.11);
                    data [index] = (luminance > threshold) ? (byte)1 : (byte)0;
                    index++;
                }
            return data;
        }

Your questions is still on a high level. I am sorry I still do not understand your question nor the code since I am personally not into image processing.

I am not sure how BitArray works but if you tell us what you are trying to do (on low level) then I can show you how to do it.

The BitArray class in the full framework is simply a set of bits. Conveniently enough, so is every other type. For example, an int is 32 bits that behave in specific ways when you use +, -, and the other operators. A byte is similarly 8 bits, and a byte would certainly accomplish what you want to do. Your code for converting a Bitmap into a BitArray will certainly not work for a byte, though.

@ godefroi
Do you have any suggestions? do you know how to convert a bitmap into byte array in micro framework

Have you looked at the Bitmap object methods? :slight_smile:


Bitmap b = new Bitmap(10, 10);
byte[] bytearray = b.GetBitmap();

Do you have documentation on the format of the image for the printer?

Hey guys,

i have a manual [url]http://www.custom.biz/downloads/depliants/DOME-VKP80II_Rev105.pdf[/url] of the printer if its not to much to ask, could somone explain te part select bit image mode on page 41. there is a vague discription of how to do this. the other commands work but i can’t seem to get the bit image mode. :frowning:

the GetBitmap i tried already but it doesn’t seem to work here.

:frowning:

Try using one byte for 8 pixels, instead of using byte for each pixel.

For what it’s worth,
this is what we used to use in our code, maybe it can help you:


private static byte[] GetBitmapBytes(Bitmap bitmap)
{
    int threshold = 127;
    int index = 0;
    int dimensions = bitmap.Height * bitmap.Width;

    byte[] data = new byte[dimensions / 8];

    for (int horizontalLine = 0; horizontalLine < bitmap.Height / 16; horizontalLine++)
        for (int column = 0; column < bitmap.Width / 8; column++)
            for (int row = horizontalLine * 16; row < 16 * horizontalLine + 16; row++)
                for (int x = 0; x < 8; x++)
                {
                    var c = bitmap.GetPixel(column * 8 + x, row);
                    var r = ColorUtility.GetRValue(c);
                    var g = ColorUtility.GetGValue(c);
                    var b = ColorUtility.GetBValue(c);
                    int luminance = (int)(r * 0.3 + g * 0.59 + b * 0.11);

                    if (luminance > threshold)
                        ByteArrayExtender.SetBit(ref data, index);

                    index++;
                }

    return data;
}

Since you want to work on a bit level, this may be of some use to you as well:


        public static void SetBit(ref byte[] source, int bit)
        {
            int b = bit / 8;
            int bitIndex = bit % 8;
            source[b] |= (byte)(1 << bitIndex);
        }

And I suggest you take a look at:
http://www.itcrowd.be/getting-started-with-ghi-rlp

Where I do the exact same thing in C with RLP, which will greatly increase performance