Click here to Skip to main content
15,885,853 members
Home / Discussions / C#
   

C#

 
AnswerRe: Is there any technique available for copying one object of class A to another object of class B ? Pin
Łukasz Nowakowski5-Aug-10 21:41
Łukasz Nowakowski5-Aug-10 21:41 
QuestionBest Way to Go About Updating Forms When A Piece of Data Is Changed Pin
Matt U.5-Aug-10 11:44
Matt U.5-Aug-10 11:44 
AnswerRe: Best Way to Go About Updating Forms When A Piece of Data Is Changed Pin
Chris Trelawny-Ross9-Aug-10 8:23
Chris Trelawny-Ross9-Aug-10 8:23 
QuestionInterface Events Pin
Ian Grech5-Aug-10 10:52
Ian Grech5-Aug-10 10:52 
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 
I have an application that spits out binary masks to file. These files are simply a long string of bits, each set to 1 or 0. There is no metadata or header; you need to know the dimensions of the mask before processing it. I would like to convert these files into bitmaps for each of visualization.

I have code that works, but it's extremely inefficient (40 seconds for a 1025x1025 pixel image). This mechanism runs through the bits one at a time, tests each one and sets the bits of a bitmap individually to black or white based on the test result. Can anyone suggest a more efficient method?

public static void FileToImage(string path)
{
    byte[] data;

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

    int i = 0;
    int numPixels = 1025;
    Bitmap bmp = new Bitmap(numPixels, numPixels);
    for (int y = 0; y < numPixels; y++)
    {
        for (int x = 0; x < numPixels; x++)
        {
            if (data[i++] == 0)
            {
                bmp.SetPixel(x, y, Color.Black);
            }
            else
            {
                bmp.SetPixel(x, y, Color.White);
            }
        }
    }

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

private static void ReadWholeArray(Stream stream, byte[] data)
{
    int offset = 0;
    int remaining = data.Length;
    while (remaining > 0)
    {
        int read = stream.Read(data, offset, remaining);
        if (read <= 0)
            throw new EndOfStreamException
                (String.Format("End of stream reached with {0} bytes left to read", remaining));
        remaining -= read;
        offset += read;
    }
}

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 
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 

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.