Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6688779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:26:33+00:00 2026-05-26T05:26:33+00:00

How can i use linq to list all pixels from image and process on

  • 0

How can i use linq to list all pixels from image and process on them?
For example we have:

 for (int i = 0; i < this.bitmap.Width; i++)
            {
                for (int j = 0; j < this.bitmap.Height; j++)
                {
                    bitmap.SetPixel(x, y, Color.FromArgb(1, 1, 1));
                }
            }

Here i do not need list pixels because i work immediately using meanwhile for loops.
However i am not sure if i can process image with linq like that.
I understand that linq gain data so, i would like to take pixel from point and somehow store coorinates and color data of that pixel. I need that because how would i be able then make threshold?

I tried to use struct but on page http://social.msdn.microsoft.com/Forums/is/linqtosql/thread/02154de8-ef32-4420-a3f6-e4e473df66ce they said that linq doesn’t work with struct.

Maybe i should use list but when i wrote
List<Point, Color> list i got error. So i really don’t know how to do that..

The all thing is about to optimalize my function under that text. I read in book called “effectice programming in c#” that using query syntax would be more readable than for loops..

This is my code and i really ask you for a help how make it more readable code.

ps. please correct topic if it is not good specified by me, sorry.

private void ThreshGrayTails()
{

        this.tempBitmap = new Bitmap(this.bitmap);

        for (int i = 0; i < this.bitmap.Width; i++)
        {
            for (int j = 0; j < this.bitmap.Height; j++)
            {
                SetTempPixelInt(i, 0, j, 0, this.tempBitmap);

                if (this.tempPixelInt == 252 && (j + 2) < this.bitmap.Height && (j - 2) > 0)
                {
                    SetTempPixelInt(i, 0, j, -1, this.bitmap);

                    //if pixel above has value 252
                    if (this.tempPixelInt == 252)
                    {
                        SetTempPixelInt(i, 0, j, -2, this.bitmap);

                        //if pixel above has value 159
                        if (this.tempPixelInt == 159)
                        {
                            SetTempPixelInt(i, 0, j, +1, this.bitmap);

                            //if pixel under has value 0 or 172
                            if (this.tempPixelInt == 0 || this.tempPixelInt == 172)
                            {
                                this.tempBitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
                                this.tempBitmap.SetPixel(i - 1, j, Color.FromArgb(255, 255, 255));
                                this.tempBitmap.SetPixel(i - 2, j, Color.FromArgb(255, 255, 255));
                            }
                        }
                    }

                }

                //if current pixel doesnt contain value in that list, turn it on black
                if (!colorsToThreshold.Contains(this.tempBitmap.GetPixel(i, j).R))
                {
                    Color newcolor = Color.FromArgb(0, 0, 0);
                    this.tempBitmap.SetPixel(i, j, newcolor);
                }
                //if current pixel contain value in that list, turn it on white
                else
                {
                    Color newcolor = Color.FromArgb(255, 255, 255);
                    this.tempBitmap.SetPixel(i, j, newcolor);
                }

            }

        }
        this.bitmap = new Bitmap(this.tempBitmap);
        SaveImage("thresholded.bmp", this.bitmap);
    }

    private void SetTempPixelInt(int i, int pi, int j, int pj, Bitmap bitmap)
    {
        Color currentColor = bitmap.GetPixel(i + pi, j + pj);
        this.tempPixelInt = (int)(currentColor.R);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:26:34+00:00Added an answer on May 26, 2026 at 5:26 am

    The all thing is about to optimalize my function under that text

    If you want to improve performance the best solution for you would be to use unsafe code to access pixels directly in memory. You should use LockBits() method on Bitmap to achieve this. Some example of fast image processing in c# can be found here: http://www.vcskicks.com/fast-image-processing.php

    LINQ is really not a solution for such a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Dictionary in C#: Dictionary<string, List<string>> How can I use Linq to
I can't use TryGetValue() from dictionary in linq expression with anonymous type. Dictionary<string, string>
So I want to use one of these LINQ functions with this List<string> I
I have this code which gives me a list of all the users by
Using EF we can use LINQ to read data which is rather simple (especially
I am wondering if you can use Linq to SQL using a .mdf file
How can I use Linq with Dataset.xsd files? I've looked at Linq-to-Datasets and Linq-to-XSD
Assuming you can't use LINQ for whatever reason, is it a better practice to
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own
I am trying to create Linq Expression that I can use to query child

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.