Creating Pixelated Images in C#


If you read my site for any length of time, you'll notice that I tend to post a lot on things like image manipulation, image recognition, video processing (in the future anyway), etc. To be honest I find the field to be rather interesting and have even started working on incorporating OpenCV into the project I'm working on... Anyway, I'm starting to add some more "fun" items to my library. No real practical use, just there for the sake of being there really. In this instance I wanted to simply create a pixelated cookie monster... I'm not going into why I wanted to create this, but it's for an April Fools joke. Anyway, I decided instead of doing the easy thing (opening up Photoshop) that I'd do it the hard way and use C#.

        public static Bitmap Pixelate(Bitmap Image, int PixelSize)
        {
            System.Drawing.Bitmap TempBitmap = Image;
            System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
            System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
            NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
            NewGraphics.Dispose();
            int TempX = 0;
            int TempY = 0;
            while (true)
            {
                int RValue = 0;
                int GValue = 0;
                int BValue = 0;
                for (int x = TempX; x < TempX + PixelSize; ++x)
                {
                    if (x >= NewBitmap.Width)
                        break;
                    for (int y = TempY; y < TempY + PixelSize; ++y)
                    {
                        if (y >= NewBitmap.Height)
                            break;
                        Color TempPixel = NewBitmap.GetPixel(x, y);
                        RValue += TempPixel.R;
                        GValue += TempPixel.G;
                        BValue += TempPixel.B;
                    }
                }
                RValue = RValue / (PixelSize * PixelSize);
                GValue = GValue / (PixelSize * PixelSize);
                BValue = BValue / (PixelSize * PixelSize);
                for (int x = TempX; x < TempX + PixelSize; ++x)
                {
                    if (x >= NewBitmap.Width)
                        break;
                    for (int y = TempY; y < TempY + PixelSize; ++y)
                    {
                        if (y >= NewBitmap.Height)
                            break;
                        NewBitmap.SetPixel(x, y, Color.FromArgb(RValue, GValue, BValue));
                    }
                }
                TempX += PixelSize;
                if (TempX + PixelSize > NewBitmap.Width)
                {
                    TempX = 0;
                    TempY += PixelSize;
                }
                if (TempY >= NewBitmap.Height)
                {
                    break;
                }
            }
            return NewBitmap;
        }

Actually that isn't that difficult really. The code above takes in a Bitmap object and the size of the individual pixels. It then does one pass over a section of that image (a section is the size in pixels specified), finds the average image color, and does another pass setting the pixels to that value. Once it's done with the image it breaks out of the loop and returns a new version of the bitmap in its pixelated glory. Anyway, I hope this helps out someone out there. So try out the code, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,
Categories: C#
Posted by James Craig on Monday, January 05, 2009 2:05 PM
Permalink | Comments (0) | Post RSSRSS comment feed

No new post


I'm posting today about the lack of posting (which will change Monday at the latest). I've had two weeks of vacation and I tried my best to sleep through it... Well that and I was kind of busy blowing up Megaton... That game is very addicting. I also ended up with a Zune to replace my iPod. I have to say that I love it thus far. I really was expecting to not like it (one of those gifts that you like where you say "Oh... It's great..."). Thus far it sounds about 10x better, seems a bit more durable (plus it hasn't hung at all unlike my iPod, although apparently the 30 gigs bricked this year), etc. Plus I really do like the radio built in as it should help at the gym (although making up the words myself on those tv programs is sometimes fun).

Anyway, I do have a little bit of code for you today. Nothing big, just some code to get a document from a web site:

            WebClient Client = null;
            StreamReader Reader = null;
            string Contents="";
            try
            {
                Client = new WebClient();
                Reader = new StreamReader(Client.OpenRead(FileName));
                Contents = Reader.ReadToEnd();
                Reader.Close();
            }
            catch
            {
            }
            finally
            {
                if (Reader != null)
                {
                    Reader.Close();
                }
            }

That's all there is to it. Just create a web client and have that download the item, just using a stream to get the file contents. That's all there is to it. Anyway, hopefully Monday I'll have something more interesting for you. For now, happy belated new year and happy coding... Also, why isn't Felicia Day the sexiest geek of 2008? I don't get that list. Kari Byron, Stephen Colbert, Tina Fey, Jade Raymond, and Danica McKellar I understand to a certain extent. Being in Hitchhiker's Guide, playing WoW, or even acting in Buffy doesn't count as geek though. Also, I've never heard of Philip DeFranco or Marina Orlova before now (YouTube fame = no fame in my book). But I still say it's rigged if Day isn't even in the top ten...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: C# | General
Posted by James Craig on Friday, January 02, 2009 1:58 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Getting an HTML Based Color Palette from an Image in C#


I'll be honest and say that I don't know if this is going to be that useful to anyone out there, but hey, it's free code. Anyway, I recently ran into a situation where I needed to get the palette that was used by an image, take that palette and convert it to HTML based colors (#012345, etc.). It's actually very simple but unless you know about the ColorTranslator class, you'd be scratching your head as to how to do it in a simple manner. Luckily for us that class exists and contains a function called ToHTML which converts a color object to an HTML based string. For our purposes, we're just going to do the following:

        public static List<string> GetHTMLPalette(Bitmap Image)
        {
            List<string> ReturnArray = new List<string>();
            if (Image.Palette != null && Image.Palette.Entries.Length > 0)
            {
                for (int x = 0; x < Image.Palette.Entries.Length; ++x)
                {
                    string TempColor = ColorTranslator.ToHtml(Image.Palette.Entries[x]);
                    if (!ReturnArray.Contains(TempColor))
                    {
                        ReturnArray.Add(TempColor);
                    }
                }
                return ReturnArray;
            }
            for (int x = 0; x < Image.Width; ++x)
            {
                for (int y = 0; y < Image.Height; ++y)
                {
                    string TempColor = ColorTranslator.ToHtml(Image.GetPixel(x, y));
                    if (!ReturnArray.Contains(TempColor))
                    {
                        ReturnArray.Add(TempColor);
                    }
                }
            }
            return ReturnArray;
        }

That's it. All the code does, is checks if there is a palette. If there is, it simply copies those values (once converted) to the return array. Otherwise it goes through each pixel and converts it that way, building up the array as it goes. Very simple and gets the job done. Once again, I don't know if this code is going to help anyone out there, but you never know. Anyway, try it out, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,
Categories: C#
Posted by James Craig on Friday, December 19, 2008 3:45 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Recent comments

None

Calendar

<<  January 2009  >>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Sponsors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009