我有两种方法完全相同,除了一个在位图上,另一个在图像上。我想只能有一种方法,因此它更干净,但我不知道如何实现这一点。如果不能将这两种方法放在一起,那么简化和压缩这些代码的最好方法是什么?将这两种图像处理方法合为一体?
谢谢!
static private Bitmap resizeBitmap(Bitmap inputBitmap, Orientation orientation) {
double scaleX = 1;
double scaleY = 1;
int pageWidth = orientation == Orientation.Portrait ? (int)PageDimensions.Width : (int)PageDimensions.Height;
int pageHeight = orientation == Orientation.Portrait ? (int)PageDimensions.Height : (int)PageDimensions.Width;
if (inputBitmap.Width > pageWidth) {
scaleX = Convert.ToDouble(pageWidth)/Convert.ToDouble(inputBitmap.Width);
scaleY = scaleX;
}
if (inputBitmap.Height * scaleY > pageHeight) {
scaleY = scaleY * Convert.ToDouble(pageHeight)/Convert.ToDouble(inputBitmap.Height);
scaleX = scaleY;
}
Bitmap outputImage = new Bitmap(Convert.ToInt16(inputBitmap.Width * scaleX), Convert.ToInt16(inputBitmap.Height * scaleY));
using (Graphics g = Graphics.FromImage(outputImage))
g.DrawImage(inputBitmap, 0, 0, outputImage.Width, outputImage.Height);
return outputImage;
}
static private Image resizeImage(Image inputImage, Orientation orientation) {
double scaleX = 1;
double scaleY = 1;
int pageWidth = orientation == Orientation.Portrait ? (int)PageDimensions.Width : (int)PageDimensions.Height;
int pageHeight = orientation == Orientation.Portrait ? (int)PageDimensions.Height : (int)PageDimensions.Width;
if (inputImage.Width > pageWidth) {
scaleX = Convert.ToDouble(pageWidth)/Convert.ToDouble(inputImage.Width);
scaleY = scaleX;
}
if (inputImage.Height * scaleY > pageHeight) {
scaleY = scaleY * Convert.ToDouble(pageHeight)/Convert.ToDouble(inputImage.Height);
scaleX = scaleY;
}
Bitmap outputImage = new Bitmap(Convert.ToInt16(inputImage.Width * scaleX), Convert.ToInt16(inputImage.Height * scaleY));
using(Graphics g = Graphics.FromImage(outputImage))
g.DrawImage(inputImage, 0, 0, outputImage.Width, outputImage.Height);
return outputImage;
}
现在这就是我所希望的,我刚刚实施了你的解决方案,它的效果非常好:) – sooprise 2011-02-18 19:17:41