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. Coloring pictures

Coloring pictures

Scheduled Pinned Locked Moved C#
graphicsquestion
6 Posts 4 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.
  • S Offline
    S Offline
    Sokka93
    wrote on last edited by
    #1

    Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance. Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }

    H D L 4 Replies Last reply
    0
    • S Sokka93

      Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance. Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }

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

      Why are you retrieving the color from pic when you don't use it? Here

      Color color = pic.GetPixel(i, j); <=============================== this never gets used.
      newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125));

      But if your newpic is to be all one color, look up the Graphics class, and the Fill method of that class.

      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
      • S Sokka93

        Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance. Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Search the articles for "Image processing for dummies" and you'll find a series of articles that covers just what you're looking for.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        1 Reply Last reply
        0
        • S Sokka93

          Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance. Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }

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

          Hi, 1. an easy win is to replace pic.Height by a variable that gets initialized only once. 2. for a fixed color, there are better ways, as Henry already mentioned. Here is one:

          Graphics g=Graphics.FromImage(newpic);
          g.FillRectangle(...);
          g.Dispose();

          3. the general solution for maximum performance is using pointers, avoiding GetPixel/SetPixel since these methods will perform boundary checks and coordinate conversions for each individual pixel. :)

          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
          • S Sokka93

            Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance. Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }

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

            I have realised that I made a typo, had a brain fart, in my previous post. I should have said If it is going to be one color investigate the Graphics class and the Clear method of that class. Although the various Fill methods are good to know.

            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.”

            S 1 Reply Last reply
            0
            • H Henry Minute

              I have realised that I made a typo, had a brain fart, in my previous post. I should have said If it is going to be one color investigate the Graphics class and the Clear method of that class. Although the various Fill methods are good to know.

              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.”

              S Offline
              S Offline
              Sokka93
              wrote on last edited by
              #6

              Thank you all for your good answers.

              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