Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Lockbits error [modified]

Lockbits error [modified]

Scheduled Pinned Locked Moved C#
helpgraphics
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gigahertz205
    wrote on last edited by
    #1

    I have a 512x512 image, and using lockbits and unsafe code, i visit each pixel to retrieve that pixel value. This is a grayscale image. What i want to do is start the retrieval from the 256th pixel in each row (or, collectively, the right half of the image). Heres what i tried:

    inputImg = Image.FromFile(inputImgPath);
    b = (Bitmap)inputImg;
    BitmapData bmData = b.LockBits(new Rectangle(256, 0, b.Width, b.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;
    unsafe
    {
    byte* p = (byte*)(void*)Scan0;
    int nOffset = stride - 256;
    for (int y = 0; y < b.Height; ++y)
    {
    for (int x = 0; x < 256; ++x)
    {
    current = (byte)p[0];
    p++;
    }
    p += nOffset;
    }
    }
    b.UnlockBits(bmData);

    The problem is occurs when i use the LockBits method. The compiler says the parameter is not valid.

    modified on Saturday, August 23, 2008 12:33 AM

    P 1 Reply Last reply
    0
    • G gigahertz205

      I have a 512x512 image, and using lockbits and unsafe code, i visit each pixel to retrieve that pixel value. This is a grayscale image. What i want to do is start the retrieval from the 256th pixel in each row (or, collectively, the right half of the image). Heres what i tried:

      inputImg = Image.FromFile(inputImgPath);
      b = (Bitmap)inputImg;
      BitmapData bmData = b.LockBits(new Rectangle(256, 0, b.Width, b.Height),
      ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
      int stride = bmData.Stride;
      System.IntPtr Scan0 = bmData.Scan0;
      unsafe
      {
      byte* p = (byte*)(void*)Scan0;
      int nOffset = stride - 256;
      for (int y = 0; y < b.Height; ++y)
      {
      for (int x = 0; x < 256; ++x)
      {
      current = (byte)p[0];
      p++;
      }
      p += nOffset;
      }
      }
      b.UnlockBits(bmData);

      The problem is occurs when i use the LockBits method. The compiler says the parameter is not valid.

      modified on Saturday, August 23, 2008 12:33 AM

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      gigahertz205 wrote:

      The compiler says the parameter is not valid.

      What parameters is it supposed to take? Does it take the Rectangle object? ---modified Try using the code block button to make your code snippet easier to read.

      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

      G 1 Reply Last reply
      0
      • P Paul Conrad

        gigahertz205 wrote:

        The compiler says the parameter is not valid.

        What parameters is it supposed to take? Does it take the Rectangle object? ---modified Try using the code block button to make your code snippet easier to read.

        "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

        G Offline
        G Offline
        gigahertz205
        wrote on last edited by
        #3

        The parameters are as follows: public System.Drawing.Imaging.BitmapData LockBits(System.Drawing.Rectangle rect, System.Drawing.Imaging.ImageLockMode flags, System.Drawing.Imaging.PixelFormat format) And the rectangle: public Rectangle(int x, int y, int width, int height) This worked when my starting point was at (0,0) and visited every single pixel in the image, but when its at (256,0), it gives me back an error. EDIT: this is where i got the code from (http://www.codeproject.com/KB/GDI-plus/csharpgraphicfilters11.aspx[^])

        P 1 Reply Last reply
        0
        • G gigahertz205

          The parameters are as follows: public System.Drawing.Imaging.BitmapData LockBits(System.Drawing.Rectangle rect, System.Drawing.Imaging.ImageLockMode flags, System.Drawing.Imaging.PixelFormat format) And the rectangle: public Rectangle(int x, int y, int width, int height) This worked when my starting point was at (0,0) and visited every single pixel in the image, but when its at (256,0), it gives me back an error. EDIT: this is where i got the code from (http://www.codeproject.com/KB/GDI-plus/csharpgraphicfilters11.aspx[^])

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          You might want to ask the author, Christian, in the said article's message board.

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups