HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Resize image with good quality and less weight

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
qualityimageresizewithweightlessgoodand

Problem

I want to resize an image with good quality, and the most important thing is having less weight for an image so that webpages will load it faster.

I am using the below method for this. Please check it out, if it is proper or let me know if it needs any modifications. If there is a better method than this, then also please tell me.

```
public static void ResizeImage(int newWidth, int newHeight, Stream sourcePath, string targetPath)
{
using (var image = System.Drawing.Image.FromStream(sourcePath))
{
Image imgPhoto = Image.FromStream(sourcePath);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;

if (sourceWidth > newWidth || sourceHeight > newHeight)
{
//Consider vertical pics
if (sourceWidth < sourceHeight)
{
int buff = newWidth;

newWidth = newHeight;
newHeight = buff;
}

int destX = 0, destY = 0;
float nPercent = 0, nPercentW = 0, nPercentH = 0;

nPercentW = ((float)newWidth / (float)sourceWidth);
nPercentH = ((float)newHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(sourceHeight * nPercent)) / 2);
}
}

var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, new

Solution


  • Variable type choice. Why do you use Int16 and float? Most likely you'll be running on a 32-bit or 64-bit machine, and even the 32-bit machines will be optimized for math operations using double. I know you don't need perfect accuracy, but using the smaller types is not likely to save you any space or time. The optimizer will probably convert them to Int32 and double anyway and then just round them off/truncate/extend.



  • You should declare variables as late as possible. Ideally right before they're initialized. In particular, there's no point in declaring a variable, setting its initial value to 0, and then on the next line setting its value to what you really wanted.



  • What is the purpose of nPercent, destX, and destY? You calculate them then throw them away.



  • I don't think you tested this code. You have an if block that swaps newWidth and newHeight, but nothing that ever swaps them back. What's the code intended to do?

Context

StackExchange Code Review Q#55698, answer score: 3

Revisions (0)

No revisions yet.