将这两种图像处理方法合二为一?

发布于 2024-10-17 20:18:32 字数 2112 浏览 2 评论 0原文

我有两种方法可以做完全相同的事情,除了一种是在位图上,另一种是在图像上。我希望能够只有一种方法,这样它就更干净,但我不知道如何实现这一点。如果无法将这两种方法放在一起,那么简化和压缩部分代码的最佳方法是什么?

谢谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

城歌 2024-10-24 20:18:32

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.

音盲 2024-10-24 20:18:32

您只需要一个接受 Image 参数并具有 Bitmap 返回类型的函数,因为您实际上在 中返回一个 Bitmap >resizeImage 方法。这是有效的,因为 Bitmap 继承自 Image

static private Bitmap resizeImage(Image inputImage, Orientation orientation) 
{
    ...
}

这样,如果您将 resizeImage 的结果分配给 Bitmap 类型的变量(我认为这是您编写这两个函数的最初原因),则不必强制转换它。

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).

伴梦长久 2024-10-24 20:18:32

在一般情况下,如果您正在处理两个没有公共基类或接口的类(并且您无法添加基类或接口),您能做的最好的事情就是将不依赖于特定类的代码提取到一个新的类中您现有的两个方法都调用的方法。例如,类似:

/// 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)

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)
似梦非梦 2024-10-24 20:18:32

我不知道它是否非常有用,但是您可以在其中一个函数中添加一个 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!

终止放荡 2024-10-24 20:18:32

我不喜欢发布我自己的问题的答案,但这似乎工作得很好,并且是一个简单的解决方案:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文