Note that there are some explanatory texts on larger screens.

plurals
  1. POTurning These Two Image Manipulation Methods Into One?
    text
    copied!<p>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?</p> <p>Thanks!</p> <pre><code>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 &gt; pageWidth) { scaleX = Convert.ToDouble(pageWidth) / Convert.ToDouble(inputBitmap.Width); scaleY = scaleX; } if (inputBitmap.Height * scaleY &gt; 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 &gt; pageWidth) { scaleX = Convert.ToDouble(pageWidth) / Convert.ToDouble(inputImage.Width); scaleY = scaleX; } if (inputImage.Height * scaleY &gt; 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload