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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
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