我有两种方法完全相同,只有一种是在Bitmap上,另一种是在Image上。我希望能够只有一种方法,所以它更干净,但我不知道如何实现这一目标。如果不可能将这两种方法放在一起,那么简化和压缩这些代码的最佳方法是什么?
谢谢!
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;
}
答案 0 :(得分:3)
我不是C#专家,但根据the documentation,Bitmap 是一个Image(它继承自它),所以你可以用Bitmap调用第二个方法作为论点,它应该工作。如果我是正确的,那么只需删除第一个,这是不再有用的。
答案 1 :(得分:1)
在一般情况下,如果您正在处理两个没有公共基类或接口的类(并且您不能添加一个),那么您可以做的最好的事情是提取不依赖于特定类的代码转换为现有方法调用的新方法。例如,像:
/// getNewDimensions
static private void getNewDimensions(int ImageWidth, int ImageHeight, Orientation orientation, out int NewWidth, out int NewHeight) {
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 (ImageWidth > pageWidth) {
scaleX = Convert.ToDouble(pageWidth) / Convert.ToDouble(ImageWidth);
scaleY = scaleX;
}
if (ImageHeight * scaleY > pageHeight) {
scaleY = scaleY * Convert.ToDouble(pageHeight) / Convert.ToDouble(ImageHeight);
scaleX = scaleY;
}
NewWidth = ImageWidth * scaleX;
NewHeight = ImageHeight * scaleY;
}
/// resizeBitmap
static private Bitmap resizeBitmap(Bitmap inputBitmap, Orientation orientation) {
int NewWidth = 0;
int NewHeight = 0;
getNewDimensions(inputBitmap.Width, inputBitmap.Width, orientation, NewWidth, NewHeight);
Bitmap outputImage = new Bitmap(Convert.ToInt16(newWidth), Convert.ToInt16(newHeight));
using (Graphics g = Graphics.FromImage(outputImage))
g.DrawImage(inputBitmap, 0, 0, outputImage.Width, outputImage.Height);
return outputImage;
}
/// resizeImage (I'll leave as an exercise to the reader)
答案 2 :(得分:1)
您只需要一个带有Image
参数且具有Bitmap
返回类型的函数,因为您实际上正在Bitmap
方法中返回resizeImage
。这可行,因为Bitmap
继承自Image
。
static private Bitmap resizeImage(Image inputImage, Orientation orientation)
{
...
}
这样,如果要将resizeImage的结果赋值给Bitmap
类型的变量(我假设这是你编写这两个函数的原因),就不必强制转换resizeImage的结果。
答案 3 :(得分:0)
我不知道它是否会非常有用但是,你可以在其中一个函数中添加一个IF,以了解你的参数是否是Bitmap或Image类型的对象。你加入了两个功能
祝你好运!答案 4 :(得分:0)
我不喜欢在我自己的问题上发布答案,但这似乎工作得很好并且是一个简单的解决方案:
static private Bitmap resizeBitmap(Bitmap inputBitmap, Orientation orientation) {
return (Bitmap)resizeImage(inputBitmap, orientation);
}
//Resizes images
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;
}