Re: Image to raw results with weird error

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



The width of a row of pixels is not necessarily the width of the row in
bytes. The actual byte width of the row is stored in the BitmapData.Stride
property. So, you need to use that to read and write the the Bitmap. If you
don't want the (possible) padding in the row, you can use the Stride minus
the Width to get the size of the difference. Then you can use that
difference to write the bytes back to the Bitmap.

See http://www.bobpowell.net/lockingbits.htm for more information.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"OGG" <itye.richter@xxxxxxxxx> wrote in message
news:1187767657.831423.57820@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi guys,

I am trying to do a simple task: convert bitmap to raw and back to
bitmap as fast as possible.
Here are the 2 methods I am using:

private byte[] ToRaw(Bitmap bmp)
{
BitmapData data = bmp.LockBits(new Rectangle(new Point(0,
0), bmp.Size), ImageLockMode.ReadOnly, bmp.PixelFormat);
byte[] buffer = new byte[data.Height * data.Width * 3];
Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);
bmp.UnlockBits(data);
return buffer;
}

private Bitmap ToBitmap(byte[] raw, int w, int h, PixelFormat
pf)
{
Bitmap bmp = new Bitmap(w, h, pf);
BitmapData data = bmp.LockBits(new Rectangle(new Point(0,
0), bmp.Size), ImageLockMode.ReadWrite, pf);
Marshal.Copy(raw, 0, data.Scan0, raw.Length);
bmp.UnlockBits(data);
return bmp;
}


It works perfectly to any image with width less than 700 pixels or
more than 799 pixels.
The strangest thing happens to a bitmap with width of 701 to 799. It
simply returns the image a little distorted, and I have no clue why.

BTW, if I use simple bitmap.GetPixel / SetPixel it works, but it takes
a lot of time.

Any idea is appreciated.

Thanks.



.



Relevant Pages

  • Re: CF and display of Images
    ... public Bitmap GetBitmapUsingImagingAPI ... Bitmap bmp = null; ... int intSize = GetBitmapDimensions; ... int dataSize = LoadBitmapData; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Problem with SavePicture, and a question on new versions
    ... > mode does not have to be in pixels. ... 500 pixels then the picture box will also be 700 x 500 pixels. ... and the bitmap shows ... to be *smaller* than the scale units that the BitBlt API is using ...
    (comp.lang.basic.visual.misc)
  • Re: How long Win32 viable?
    ... >> monitor pixels. ... the pixel based world we have with the Win32 API approach to graphics. ... would translate your comment as meaning that your apps are designed for one ... expecially when the bitmap has a lot more pixels than the form it is ...
    (borland.public.delphi.non-technical)
  • Anti-aliasing idea for bitmap
    ... How to edit a button's bitmap so that only the button ... I load an 8-bit bitmap as a resource. ... and edit its pixels depending on which state the button is in. ... Problem: No anti-aliasing. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: how to get a bitmap from the screen and then inside lisp?
    ... Getting pixels off the screen quickly is a bit complicated and you ... have to do some decoding of Windows bitmap data structures. ... int x, int y, int w, int h, ... // get the window handle ...
    (comp.lang.lisp)