patterncsharpMinor
Resize image with good quality and less weight
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
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
Int16andfloat? 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 usingdouble. 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 toInt32anddoubleanyway 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, anddestY? You calculate them then throw them away.
- I don't think you tested this code. You have an
ifblock that swapsnewWidthandnewHeight, 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.