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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. urgent help pleasse [image processing , pointers , byte ]

urgent help pleasse [image processing , pointers , byte ]

Scheduled Pinned Locked Moved C#
helpgraphics
7 Posts 5 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.
  • W Offline
    W Offline
    WinSolution
    wrote on last edited by
    #1

    Hi Everyone please look at following two line this is not working when i execute my prject int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); but if i remove the first line and enter an integer value in place of div variable it works such as following line. ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3); Following is the full listing may it help you to figurout the problem a method that convert a bitmap from colored to grayscale ////////////////////////////////////////////////////////////////////////// public Bitmap GrayScale(Bitmap bmpimg) { BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int remain = bmpData.Stride - bmpData.Width * 3; unsafe { byte* ptr = (byte*)bmpData.Scan0; for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); ptr += 3; } ptr += remain; } } bmpimg.UnlockBits(bmpData); return bmpimg; }

    L H W S 4 Replies Last reply
    0
    • W WinSolution

      Hi Everyone please look at following two line this is not working when i execute my prject int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); but if i remove the first line and enter an integer value in place of div variable it works such as following line. ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3); Following is the full listing may it help you to figurout the problem a method that convert a bitmap from colored to grayscale ////////////////////////////////////////////////////////////////////////// public Bitmap GrayScale(Bitmap bmpimg) { BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int remain = bmpData.Stride - bmpData.Width * 3; unsafe { byte* ptr = (byte*)bmpData.Scan0; for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); ptr += 3; } ptr += remain; } } bmpimg.UnlockBits(bmpData); return bmpimg; }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      :confused: :confused: :confused: why is int div = Convert.ToInt32(textBox1.Text.Trim()); inside the nested loops? Are you planning on changing div while the image processing is going on? or are you just looking for ways to make it extremely slow? :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      1 Reply Last reply
      0
      • W WinSolution

        Hi Everyone please look at following two line this is not working when i execute my prject int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); but if i remove the first line and enter an integer value in place of div variable it works such as following line. ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3); Following is the full listing may it help you to figurout the problem a method that convert a bitmap from colored to grayscale ////////////////////////////////////////////////////////////////////////// public Bitmap GrayScale(Bitmap bmpimg) { BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int remain = bmpData.Stride - bmpData.Width * 3; unsafe { byte* ptr = (byte*)bmpData.Scan0; for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); ptr += 3; } ptr += remain; } } bmpimg.UnlockBits(bmpData); return bmpimg; }

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #3

        WinSolution wrote:

        this is not working when i execute my prject

        What does this mean? Are you getting an exception? Does it do something you do not expect? Have you single stepped, to see what value div is set to in your code? What, which, something else? BTW there are several articles here on CP that demonstrate converting to greyscale, have you looked at them.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        1 Reply Last reply
        0
        • W WinSolution

          Hi Everyone please look at following two line this is not working when i execute my prject int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); but if i remove the first line and enter an integer value in place of div variable it works such as following line. ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3); Following is the full listing may it help you to figurout the problem a method that convert a bitmap from colored to grayscale ////////////////////////////////////////////////////////////////////////// public Bitmap GrayScale(Bitmap bmpimg) { BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int remain = bmpData.Stride - bmpData.Width * 3; unsafe { byte* ptr = (byte*)bmpData.Scan0; for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); ptr += 3; } ptr += remain; } } bmpimg.UnlockBits(bmpData); return bmpimg; }

          W Offline
          W Offline
          WinSolution
          wrote on last edited by
          #4

          Thanks both problem solved i was not handling exception and not i placed that code outside of the loops as follow public Bitmap GrayScale(Bitmap bmpimg) { BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int remain = bmpData.Stride - bmpData.Width * 3; unsafe { byte* ptr = (byte*)bmpData.Scan0; int div = 1; try { div = Convert.ToInt32(textBox1.Text.Trim()); } catch (Exception ex) { MessageBox.Show(ex.Message); } for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); ptr += 3; } ptr += remain; } } bmpimg.UnlockBits(bmpData); return bmpimg; }

          L C 2 Replies Last reply
          0
          • W WinSolution

            Thanks both problem solved i was not handling exception and not i placed that code outside of the loops as follow public Bitmap GrayScale(Bitmap bmpimg) { BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int remain = bmpData.Stride - bmpData.Width * 3; unsafe { byte* ptr = (byte*)bmpData.Scan0; int div = 1; try { div = Convert.ToInt32(textBox1.Text.Trim()); } catch (Exception ex) { MessageBox.Show(ex.Message); } for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); ptr += 3; } ptr += remain; } } bmpimg.UnlockBits(bmpData); return bmpimg; }

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, when you store the value of bmpData.Width in a local variable, your code will run some 20% faster. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            1 Reply Last reply
            0
            • W WinSolution

              Thanks both problem solved i was not handling exception and not i placed that code outside of the loops as follow public Bitmap GrayScale(Bitmap bmpimg) { BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int remain = bmpData.Stride - bmpData.Width * 3; unsafe { byte* ptr = (byte*)bmpData.Scan0; int div = 1; try { div = Convert.ToInt32(textBox1.Text.Trim()); } catch (Exception ex) { MessageBox.Show(ex.Message); } for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); ptr += 3; } ptr += remain; } } bmpimg.UnlockBits(bmpData); return bmpimg; }

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              WinSolution wrote:

              div = Convert.ToInt32(textBox1.Text.Trim());

              You should use int.TryParse to test if the entered text is a number or not.

              Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

              1 Reply Last reply
              0
              • W WinSolution

                Hi Everyone please look at following two line this is not working when i execute my prject int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); but if i remove the first line and enter an integer value in place of div variable it works such as following line. ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3); Following is the full listing may it help you to figurout the problem a method that convert a bitmap from colored to grayscale ////////////////////////////////////////////////////////////////////////// public Bitmap GrayScale(Bitmap bmpimg) { BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int remain = bmpData.Stride - bmpData.Width * 3; unsafe { byte* ptr = (byte*)bmpData.Scan0; for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { int div = Convert.ToInt32(textBox1.Text.Trim()); ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div); ptr += 3; } ptr += remain; } } bmpimg.UnlockBits(bmpData); return bmpimg; }

                S Offline
                S Offline
                saurabh sahay
                wrote on last edited by
                #7

                You may also handle the exception for ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div);

                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