IMAGE MANIPULATION C#

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 1155  View(s)
Ratings:
Rate this:

I have the following code which takes an array of bytes which i generated and writes them out to this bitmap. If i set the pixel format to Format4bppIndexed, then i get a readable image repeating width wise 4 times, if i set it to Format1bppIndexed(which is the correct setting) then i get one big unreadable image.

The image was a decoded Jbig2 image , i know the bytes are correct i can't seem to figure out how to get it into a 1bpp readable format.

Does anyone have any advice on that matter

        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format1bppIndexed);

        //Create a BitmapData and Lock all pixels to be written          

        BitmapData bmpData = bitmap.LockBits(

                             new Rectangle(0, 0, bitmap.Width, bitmap.Height),

                             ImageLockMode.WriteOnly, bitmap.PixelFormat);

        //Copy the data from the byte array into BitmapData.Scan0

        Marshal.Copy(newarray, 0, bmpData.Scan0, newarray.Length);

        //Unlock the pixels

        bitmap.UnlockBits(bmpData);

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Image Manipulation c#

    Hi Ankita,


    I used the WPF classes instead of the GDI and wrote the code like this

    var bitmap = new WriteableBitmap(width, height, 96, 96,           System.Windows.Media.PixelFormats.BlackWhite, null);

    bitmap.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), newarray, stride, 0);    

    MemoryStream stream3 = new MemoryStream();

    var encoder = new TiffBitmapEncoder ();

    encoder.Frames.Add(BitmapFrame.Create(bitmap));

    encoder.Save(stream3);

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!