How do I read a 512x512 8-bit grey scale raw image file into a picturebox and display it in a small window?

Posted on 2005-04-21
Last Modified: 2011-09-20
I have a 512x512 pixel, 8-bit raw, greyscale bitmap image file and would like to display and fit it in a small window.  How do I do it?
Question by:pcssecure
    LVL 96

    Expert Comment

    by:Bob Learned
    Where did this image come from?

    LVL 3

    Expert Comment

    Is this a web app or a windows app?  And is the image a standard image file type (ie bmp, gif, jpg)?

    If this is a web app, you can use client side script (javascript) to force a to your specifications.  If the images change repeatedly, the script can easily be instantiated from server side code.

    In the event method where you want to launch the image, use:

    string script = @"<script language='javascript'>'<image path>','windowName','width=512,height=512,scrollbars=false,toolbar=false,statusbar=false');</script>";
    RegisterStartupScript("showimage", script);

    On a button "submit" event, this will open a new window sized to your image with no toolbars, statusbar or scollbars.

    Hope this helps.

    LVL 10

    Accepted Solution

    The following code will build a byte array with 262144 (512x512)  elements..

    It then displays it onto a graphics device... it can be scaled using the parameters passed into the DrawImage method.

          byte[] id=new byte[512*512];
          int ThisPixel;
          int c1,c2;
          for (c1=0 ; c1<512 ; c1++)
                for (c2=0 ; c2<512 ; c2++)
          // We now have a nice pattern in the data
          Bitmap bm=new Bitmap(512,512,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
          for (c1=0 ; c1<512 ; c1++)
                for (c2=0 ; c2<512 ; c2++)
                      bm.SetPixel(c1,c2,Color.FromArgb(ThisPixel,ThisPixel,ThisPixel)); // Grayscale equivalent
          e.Graphics.DrawImage(bm,50,50,100,100); // The size
          bm.Dispose(); // Tidy up


    Author Comment

    Thanks smegghead.  Your code works but the SetPixel call is too slow.  Takes about 1-2s to load a 512x512 image file.  I found some unmanaged code as well that does a better job.  Anyway, I'll give you points.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now