Hack Forums
[C#] PictureBox - Resizing an image and making it pixelate? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] PictureBox - Resizing an image and making it pixelate? (/showthread.php?tid=2903102)



PictureBox - Resizing an image and making it pixelate? - Quad - Sep 29, 2012

I download an image to a PictureBox using
PHP Code

<?php 
pictureBox1
.ImageLocation = "url"; 

It downloads an image that's 64 by 32, but my PictureBox is 256 by 128.
I have the SizeMode property set to StretchImage.

StretchImage works fine, except it blurs the edges between pixels, I need this to look pixely.

I've read a lot of answers to this questions, they all say that you need to use the Graphics class but I haven't seen any code example that have worked.

Can someone here give me the code to make the result look pixely?

Thanks a bunch 🙂


* - n0pcode - Sep 29, 2012

What you do is basically every pixel times 4, but since we have modern displays it doesn't look pixelated but blurry indeed.

If you want your image to be pixelated you'll have to delete (or skip) pixels, this is quite interesting. let me try something out.

EDIT: Could you PM me or link the image here ? So I got something to work with...


* - Quad - Sep 29, 2012

(Sep 29, 2012 04:01 PM)kript0x Wrote:  What you do is basically every pixel times 4, but since we have modern displays it doesn't look pixelated but blurry indeed.

If you want your image to be pixelated you'll have to delete (or skip) pixels, this is quite interesting. let me try something out.

EDIT: Could you PM me or link the image here ? So I got something to work with...

Alright, cool.
Hope you come up with something :)


* - Pussy Cat - Sep 29, 2012

I was bored so I came up with something, not sure if it's the best way to do this but I hope it'll help.

Code
private Image pixelate(Image img, int finalWidth, int finalHeight)
  {
    Bitmap b = (Bitmap)img;
    Size ratio = new Size(finalWidth / b.Width, finalHeight / b.Height);
    Bitmap bp = new Bitmap(b.Width * ratio.Width, b.Height * ratio.Height);
    Graphics g = Graphics.FromImage(bp);
    for (int y = 0; y < b.Height; y++)
    {
    for (int x = 0; x < b.Width; x++)
    {
    g.FillRectangle(new SolidBrush(b.GetPixel(x, y)), new Rectangle(new Point(x * ratio.Width, y * ratio.Height), ratio));
    }
    }
    return (Image)bp;
  }

Here's comparsion
[Image: 19Rbt]


* - Quad - Sep 29, 2012

(Sep 29, 2012 04:32 PM)Woozie Wrote:  I was bored so I came up with something, not sure if it's the best way to do this but I hope it'll help.

Code
private Image pixelate(Image img, int finalWidth, int finalHeight)
  {
    Bitmap b = (Bitmap)img;
    Size ratio = new Size(finalWidth / b.Width, finalHeight / b.Height);
    Bitmap bp = new Bitmap(b.Width * ratio.Width, b.Height * ratio.Height);
    Graphics g = Graphics.FromImage(bp);
    for (int y = 0; y < b.Height; y++)
    {
    for (int x = 0; x < b.Width; x++)
    {
    g.FillRectangle(new SolidBrush(b.GetPixel(x, y)), new Rectangle(new Point(x * ratio.Width, y * ratio.Height), ratio));
    }
    }
    return (Image)bp;
  }

Here's comparsion
[Image: 19Rbt]

It works like a dream, thanks man :D
Check your rep


* - Linux. - Sep 29, 2012

Can I asked why you needed to do this? It just seems like something dumb unless you are doing what I think your doing.


* - Quad - Sep 29, 2012

(Sep 29, 2012 04:54 PM)Crypto™ Wrote:  Can I asked why you needed to do this? It just seems like something dumb unless you are doing what I think your doing.

What do you think I'm doing? I wanna know 😝
I'll post a thread about this project in a minute.


* - Linux. - Sep 29, 2012

Some type of color detection program or something? Maybe something to fix the pixels so they fix themselves when re-sized.


* - Quad - Sep 29, 2012

(Sep 29, 2012 04:57 PM)Crypto™ Wrote:  Some type of color detection program or something? Maybe something to fix the pixels so they fix themselves when re-sized.

It's actually a skin stealer for Minecraft :P


* - Linux. - Sep 29, 2012

(Sep 29, 2012 04:59 PM)Quad Wrote:  It's actually a skin stealer for Minecraft :P

Really? How the hell? Add me on Skype austin--sweat 🤝


* - Debroski - Sep 29, 2012

(Sep 29, 2012 04:59 PM)Quad Wrote:  It's actually a skin stealer for Minecraft :P

May i ask why you need to pixelate an image if you want to steal a skin? You mean from other people? if so you can just download the image from http://minecraft.net/skin/PersonsName*can use textbox to change if wanted*.png, course then again that's when minecraft.net is actually up :P


* - Quad - Sep 29, 2012

(Sep 29, 2012 06:29 PM)Shinoowns Wrote:  May i ask why you need to pixelate an image if you want to steal a skin? You mean from other people? if so you can just download the image from http://minecraft.net/skin/PersonsName*can use textbox to change if wanted*.png, course then again that's when minecraft.net is actually up :P

I want to display it nicely. msg ...