将这两种图像处理方法合二为一?
我有两种方法可以做完全相同的事情,除了一种是在位图上,另一种是在图像上。我希望能够只有一种方法,这样它就更干净,但我不知道如何实现这一点。如果无法将这两种方法放在一起,那么简化和压缩部分代码的最佳方法是什么?
谢谢!
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;
}
I have two methods that do the exact same thing except one is on a Bitmap and the other is on an Image. I want to be able to just have one method so it's cleaner, but I don't know how to accomplish this. If it's not possible to put these two methods together, what would be the best way to simplify and condense some of this code?
Thanks!
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不是 C# 专家,但根据文档,位图是图像(它继承自它),因此您可以使用位图作为参数调用第二个方法,它应该可以工作。如果我是对的,那么只需删除第一个,它不再有用了。
I'm not a C# expert, but according to the documentation, a Bitmap is an Image (it inherits from it), so you could just call the second method with a Bitmap as argument, and it should work. If I'm correct, then just remove the first one, which is not useful anymore.
您只需要一个接受
Image
参数并具有Bitmap
返回类型的函数,因为您实际上在中返回一个
方法。这是有效的,因为Bitmap
>resizeImageBitmap
继承自Image
。这样,如果您将 resizeImage 的结果分配给 Bitmap 类型的变量(我认为这是您编写这两个函数的最初原因),则不必强制转换它。
You only need a single function that takes an
Image
argument and has aBitmap
return type, since you are actually returning aBitmap
in theresizeImage
method. This works sinceBitmap
inherits fromImage
.This way you don't have to cast the result of resizeImage if you are assigning it to a variable of type
Bitmap
(which I assume is the original reason you wrote both functions).在一般情况下,如果您正在处理两个没有公共基类或接口的类(并且您无法添加基类或接口),您能做的最好的事情就是将不依赖于特定类的代码提取到一个新的类中您现有的两个方法都调用的方法。例如,类似:
In the general case, if you were dealing with two classes which have no common base class or interface (and you cannot add one), the best you can do is to pull out the code that is not dependent on the specific class into a new method that both of your existing methods call. For example, something like:
我不知道它是否非常有用,但是您可以在其中一个函数中添加一个 IF 来了解您的参数是否是位图或图像类型的对象。然后您将这两个功能结合在一起
祝您好运!
I don't know if it will be very useful but, you can, in one of those function, add an IF to know if your parameter is an object of type Bitmap or Image. and you join the 2 functions together
Good luck!
我不喜欢发布我自己的问题的答案,但这似乎工作得很好,并且是一个简单的解决方案:
I don't like posting answers to my own questions, but this seems to be working pretty well and is a simple solution: