Click here to Skip to main content
16,017,151 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
http://img29.imageshack.us/img29/3247/thisjs.png

I have a question of scaling images.
When I scale an image with 1x7 in size to 82x7 for example,
it will be scaled but with a gradient of colour.
How can I solve it?

I am using graphic method to display the image.
And using the Graphics InterpolationMode cannot solve this problem.

Here is my code:
C#
Bitmap top_left = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_top_left@n.tga");

Bitmap top_center = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_top_center@n.tga");

Bitmap top_right = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_top_right@n.tga");

Bitmap center_left = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_center_left@n.tga");

Bitmap center_center = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_center_center@n.tga");

Bitmap center_right = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_center_right@n.tga");

Bitmap bottom_left = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_bottom_left@n.tga");

Bitmap bottom_center = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_bottom_center@n.tga");

Bitmap bottom_right = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_bottom_right@n.tga");


Rectangle tlr1 = new Rectangle(0, 0, top_left.Width, top_left.Height);

e.Graphics.DrawImage(top_left, tlr1);


Rectangle tcr1 = new Rectangle(top_left.Width, 0, (ClientSize.Width - (top_left.Width + top_right.Width)), top_left.Height);

e.Graphics.DrawImage(top_center, tcr1);


Rectangle trr1 = new Rectangle((ClientSize.Width - top_left.Width), 0, top_right.Width, top_right.Height);

e.Graphics.DrawImage(top_right, trr1);


Rectangle clr1 = new Rectangle(0, top_left.Height, center_left.Width, (ClientSize.Height - (top_left.Height + bottom_left.Height)));

e.Graphics.DrawImage(center_left, clr1);


Rectangle ccr1 = new Rectangle(center_left.Width, top_left.Height, (ClientSize.Width - (center_left.Width + center_right.Width)), (ClientSize.Height - (top_left.Height + bottom_left.Height)));

e.Graphics.DrawImage(center_center, ccr1);


Rectangle crr1 = new Rectangle((ClientSize.Width - center_left.Width), top_left.Height, center_right.Width, (ClientSize.Height - (top_left.Height + bottom_right.Height)));

e.Graphics.DrawImage(center_right, crr1);


Rectangle blr1 = new Rectangle(0, (ClientSize.Height - top_left.Height), center_left.Width, bottom_left.Width);

e.Graphics.DrawImage(bottom_left, blr1);


Rectangle bcr1 = new Rectangle(bottom_left.Width, (ClientSize.Height - top_left.Height), (ClientSize.Width - (bottom_left.Width + bottom_right.Width)), bottom_center.Height);

e.Graphics.DrawImage(bottom_center, bcr1);


Rectangle brr1 = new Rectangle((ClientSize.Width - bottom_left.Width), (ClientSize.Height - top_left.Height), bottom_right.Width, bottom_right.Height);

e.Graphics.DrawImage(bottom_right, brr1);
Posted
Updated 24-Mar-12 3:27am
v7

Using extension method, you can achieve it. Here is the sample

C#
public static Image GetImageHiQualityResized(this Image image, int width, int height)
{
    var thumb = new Bitmap(width, height);
    using (var g = Graphics.FromImage(thumb))
    {
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.InterpolationMode = InterpolationMode.High;
        g.DrawImage(image, new Rectangle(0, 0, thumb.Width, thumb.Height));
        return thumb;
    }
}

using(var original = Image.FromFile(@"C:\myimage.jpg"))
using(var thumb = image.GetImageHiQualityResized(120, 80))
{
    thumb.Save(@"C:\mythumb.png", ImageFormat.Png);
}


MSDN reference is available at: http://msdn.microsoft.com/en-us/library/84767bxk.aspx[^]
 
Share this answer
 
Comments
jackyng0123 24-Mar-12 9:23am    
I am not quite sure how to use your method...
But it seems to be no difference.
I have solved it.
Just adding this two lines then finish.
C#
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900