Click here to Skip to main content
16,016,140 members
Home / Discussions / C#
   

C#

 
AnswerRe: Interface Events Pin
PIEBALDconsult5-Aug-10 16:56
mvePIEBALDconsult5-Aug-10 16:56 
AnswerRe: Interface Events Pin
Chris Trelawny-Ross9-Aug-10 8:53
Chris Trelawny-Ross9-Aug-10 8:53 
GeneralRe: Interface Events Pin
Ian Grech13-Aug-10 0:34
Ian Grech13-Aug-10 0:34 
QuestionEfficient way to create a bitmap from a stream of bits in a file Pin
Adam Covitch5-Aug-10 10:50
Adam Covitch5-Aug-10 10:50 
AnswerRe: Efficient way to create a bitmap from a stream of bits in a file Pin
Eddy Vluggen5-Aug-10 11:31
professionalEddy Vluggen5-Aug-10 11:31 
AnswerRe: Efficient way to create a bitmap from a stream of bits in a file Pin
harold aptroot5-Aug-10 11:43
harold aptroot5-Aug-10 11:43 
AnswerRe: Efficient way to create a bitmap from a stream of bits in a file [modified] Pin
Luc Pattyn5-Aug-10 12:03
sitebuilderLuc Pattyn5-Aug-10 12:03 
GeneralRe: Efficient way to create a bitmap from a stream of bits in a file Pin
Adam Covitch9-Aug-10 7:02
Adam Covitch9-Aug-10 7:02 
All good suggestion. Thank you guys!

I was hoping to avoid LockBits, since this technique presupposes that the developer understands the in-memory representation of each PixelFormat. I didn't have that knowledge, and didn't want to learn it unless absolutely necessary. But it turns out that Bitmap.SetPixel() performs a lock and unlock every time a bit is set, which is hugely inefficient. So it appeared I had no choice...

So I bit the bullet, hopped on Google and found everything I needed to know at Bob Powell's site: http://www.bobpowell.net/lockingbits.htm[^]. Here is the resulting code:

public static void FileToImage(string path, int imageHeight, int imageWidth)
{
    byte[] data;

    using (FileStream fs = File.OpenRead(path))
    {
        data = new byte[fs.Length];
        ReadWholeArray(fs, data);
    }

    // Lock
    Bitmap bmp = new Bitmap(imageWidth, imageHeight, PixelFormat.Format1bppIndexed);
    Rectangle bounds = new Rectangle(Point.Empty, bmp.Size);
    BitmapData bmpData = bmp.LockBits(bounds, ImageLockMode.WriteOnly, bmp.PixelFormat);

    // Copy flat bits list into locked bitmap
    int iBitList = 0;
    unsafe
    {
        Byte* bmpDataPointer = (Byte*)bmpData.Scan0.ToPointer();

        for (int y = 0; y < imageHeight; y++)
        {
            for (int x = 0; x < imageWidth; x++)
            {
                // Since this is a B&W image (PixelFormat.Format1bppIndexed),
                // Only populate the most significant bit instead of RGB.
                int iBitmap = y * bmpData.Stride + (x >> 3);
                byte mask = (byte)(0x80 >> (x & 0x7));

                if (0x0 == data[iBitList])
                {
                    // Black
                    //bmpDataPointer[iBitmap] &= (byte)(mask^0xff);
                }
                else
                {
                    // White
                    bmpDataPointer[iBitmap] |= mask;
                }

                // Incrememnt index of flat bit list
                iBitList++;
            }
        }
    }

    // Unlock
    bmp.UnlockBits(bmpData);

    // Save
    bmp.Save(path + ".bmp");
}


When the BitmapData object is constructed, it automatically sets all pixels to 0 (black), so setting just the white pixels is perfectly valid.

That being said, this technique is so fast that it actually make no difference at all. Processing three 1025x1025 pixel images took 93 ms either way. Incidentally, this is over 400 times faster than Bitmap.SetPixel(). Wow!

Just remember - this uses pointers and unsafe code. Select 'Allow unsafe code' in the project properties (Build tab).
GeneralRe: Efficient way to create a bitmap from a stream of bits in a file Pin
Luc Pattyn9-Aug-10 7:28
sitebuilderLuc Pattyn9-Aug-10 7:28 
GeneralRe: Efficient way to create a bitmap from a stream of bits in a file Pin
Adam Covitch9-Aug-10 7:34
Adam Covitch9-Aug-10 7:34 
GeneralRe: Efficient way to create a bitmap from a stream of bits in a file Pin
Luc Pattyn9-Aug-10 7:48
sitebuilderLuc Pattyn9-Aug-10 7:48 
Questionclass function Pin
v_neil875-Aug-10 10:46
v_neil875-Aug-10 10:46 
AnswerRe: class function Pin
Pete O'Hanlon5-Aug-10 12:16
mvePete O'Hanlon5-Aug-10 12:16 
QuestionInvoking C code from C# Pin
tamash_ionut5-Aug-10 9:59
tamash_ionut5-Aug-10 9:59 
AnswerRe: Invoking C code from C# Pin
DaveyM695-Aug-10 10:06
professionalDaveyM695-Aug-10 10:06 
AnswerRe: Invoking C code from C# Pin
Luc Pattyn5-Aug-10 12:15
sitebuilderLuc Pattyn5-Aug-10 12:15 
QuestionProgram installer to require a specific Windows OS? Pin
uhcafigdc5-Aug-10 6:15
uhcafigdc5-Aug-10 6:15 
AnswerRe: Program installer to require a specific Windows OS? Pin
Not Active5-Aug-10 6:32
mentorNot Active5-Aug-10 6:32 
GeneralRe: Program installer to require a specific Windows OS? Pin
uhcafigdc5-Aug-10 7:11
uhcafigdc5-Aug-10 7:11 
GeneralRe: Program installer to require a specific Windows OS? Pin
Not Active5-Aug-10 7:29
mentorNot Active5-Aug-10 7:29 
QuestionRe: Program installer to require a specific Windows OS? Pin
uhcafigdc5-Aug-10 9:27
uhcafigdc5-Aug-10 9:27 
AnswerRe: Program installer to require a specific Windows OS? Pin
Not Active5-Aug-10 9:35
mentorNot Active5-Aug-10 9:35 
AnswerRe: Program installer to require a specific Windows OS? Pin
PIEBALDconsult5-Aug-10 17:03
mvePIEBALDconsult5-Aug-10 17:03 
GeneralRe: Program installer to require a specific Windows OS? Pin
Bernhard Hiller5-Aug-10 20:41
Bernhard Hiller5-Aug-10 20:41 
GeneralRe: Program installer to require a specific Windows OS? Pin
PIEBALDconsult6-Aug-10 3:04
mvePIEBALDconsult6-Aug-10 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.