How to resize a bitmap image in C# without blending or filtering?
Solution 1
Try to set interpolation mode:
g.InterpolationMode = InterpolationMode.NearestNeighbor;
Solution 2
I too was looking to do something similar. When I found this question neither answer were quite what I was looking for but combined they were. This is what got me to where I wanted to be and from what I can tell from your question what you want.
private Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height)
{
Bitmap result = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(result))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(sourceBMP, 0, 0, width, height);
}
return result;
}
Solution 3
May be this could help!
http://www.codeproject.com/Articles/191424/Resizing-an-Image-On-The-Fly-using-NET
Else can you please implement this method and see whether it works for you?
private static Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height )
{
Bitmap result = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(result))
g.DrawImage(sourceBMP, 0, 0, width, height);
return result;
}
hrh
My Name Is Holly, I have been interested in programming and computers for as long as I can remember. I went to collage in Madison WI and got my bachelors of Science in Computer Information Sciences in 2009. I have been working for a small company in Madison since 2007. At my job I started in c++ but now I work mostly in c#. As far as project types I've mostly done automation of daily tasks and work with a MySQL database.
Updated on June 04, 2022Comments
-
hrh almost 2 years
I have a gray scale image that I would like to enlarge so that I can better see the individual pixels. I've tried setting Smoothing mode to none and some different Interpolation Modes (as suggested on other questions on here), but the images still appear to me as if they are still doing some sort of blending before being displayed on the screen.
basically If I have a image that is
(White, White, White, Black)
I want when I enlarge it to say 6x6, it to look like
(White, White, White, White, White, White White, White, White, White, White, White White, White, White, White, White, White White, White, White, Black, Black, Black White, White, White, Black, Black, Black White, White, White, Black, Black, Black)
With no fading between the black and white areas, should look like a square. The image should look more "pixelized" rather then "Blurry"
-
hrh almost 12 yearsThis seems close but not quite right. Starting with a 2x2 pixel square, I do end up with four different regions but they don't all appear to be the same size.
-
hrh almost 12 yearsFigured it out I needed to also add: g3.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
-
AYK over 11 yearsA little explanation of your code should help the author understand quickly.
-
theGreenCabbage over 10 yearsThank you so much for this. Spent a couple hours trying to figure it out, trying to convert
bmp
tographics
and back tobmp
in order to leverage theInterpolationMode
, but your method made it work for it with absolutely no compression! Thank you!! -
SDJSK almost 8 years@hrh you're so great!