Turning These Two Image Manipulation Methods Into One?
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 = s开发者_JAVA百科caleY * 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'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.
You only need a single function that takes an Image
argument and has a Bitmap
return type, since you are actually returning a Bitmap
in the resizeImage
method. This works since Bitmap
inherits from Image
.
static private Bitmap resizeImage(Image inputImage, Orientation orientation)
{
...
}
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:
/// 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)
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:
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;
}
精彩评论