0

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?

ctacke
  • 66,480
  • 18
  • 94
  • 155
Mr. Lame
  • 1,247
  • 1
  • 17
  • 27

2 Answers2

4

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Beat me to it because I looked up if they were available on the Compact Framework =) – colithium Jul 04 '09 at 20:06
  • I tried, but MSDN wasn't terribly clear on the matter... at least not the offline version I was using. – Jon Skeet Jul 04 '09 at 20:15
  • 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 Jul 04 '09 at 20:25
  • 1
    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 Jul 04 '09 at 21:40
1

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
  • 10,269
  • 5
  • 42
  • 57