views:

1170

answers:

2

I have a Bitmap that I want to enlarge programatically to ~1.5x or 2x to its original size. Is there an easy way to do that under .NET CF 2.0?

+2  A: 

One "normal" way would be to create a new Bitmap of the desired size, create a Graphics for it and then draw the old image onto it with Graphics.DrawImage(Point, Rectangle). Are any of those calls not available on the Compact Framework?

EDIT: Here's a short but complete app which works on the desktop:

using System;
using System.Drawing;

class Test
{
    static void Main()
    {
        using (Image original = Image.FromFile("original.jpg"))
        using (Bitmap bigger = new Bitmap(original.Width * 2,
                                   original.Height * 2,
                                   original.PixelFormat))
        using (Graphics g = Graphics.FromImage(bigger))
        {
            g.DrawImage(original, new Rectangle(Point.Empty, bigger.Size));
            bigger.Save("bigger.jpg");
        }
    }
}

Even though this works, there may well be better ways of doing it in terms of interpolation etc. If it works on the Compact Framework, it would at least give you a starting point.

Jon Skeet
Beat me to it because I looked up if they were available on the Compact Framework =)
colithium
I tried, but MSDN wasn't terribly clear on the matter... at least not the offline version I was using.
Jon Skeet
I wonder what InterpolationMode it uses when you just construct a Bitmap like that and ask it to scale it. Again, MSDN isn't clear...
colithium
@Mr. Lame - thanks, will remove that option :)
Jon Skeet
Works but requires to fix few errors concerning CF compatibility:1) Bitmap.PixelFormat property does not exist 2) Rectangle has only the constructor Rectangle(x,y,width,height)3) g.DrawImage does not have the simple override. You need to call this: g.DrawImage(original, new Rectangle(0, 0, bigger.Width, bigger.Height), new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
Mr. Lame
+1  A: 

The CF has access to the standard Graphics and Bitmap objects like the full framework.

  • Get the original image into a Bitmap
  • Create a new Bitmap of the desired size
  • Associate a Graphics object with the NEW Bitmap
  • Call g.DrawImage() with the old image and the overload to specify width/height
  • Dispose of things

Versions: .NET Compact Framework Supported in: 3.5, 2.0, 1.0

colithium