我有兩種方法完全相同,除了一個在位圖上,另一個在圖像上。我想只能有一種方法,因此它更乾淨,但我不知道如何實現這一點。如果不能將這兩種方法放在一起,那麼簡化和壓縮這些代碼的最好方法是什麼?將這兩種圖像處理方法合爲一體?
謝謝!
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