J
John
Hi
I am using below code to proportionally adjust size of an image size to a
given max height & width. Problem is that image is deteriorated compared to
a similar operation in say MS Access report. Is there a better proportional
resizing function available that can maintain better image quality?
Many Thanks
Regards
01.public Bitmap ProportionallyResizeBitmap(Bitmap src, int maxWidth, int
maxHeight)
02.{
03. // original dimensions
04. int w = src.Width;
05. int h = src.Height;
06. // Longest and shortest dimension
07. int longestDimension = (w > h) ? w : h;
08. int shortestDimension = (w < h) ? w : h;
09. // propotionality
10. float factor = (float)longestDimension / shortestDimension;
11. // default width is greater than height
12. double newWidth = maxWidth;
13. double newHeight = maxWidth / factor;
14. // if height greater than width recalculate
15. if (w < h) {
16. newWidth = maxHeight / factor;
17. newHeight = maxHeight;
18. }
19. // Create new Bitmap at new dimensions
20. Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
21. using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
{
22. g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
23. }
24. return result;
25.}
I am using below code to proportionally adjust size of an image size to a
given max height & width. Problem is that image is deteriorated compared to
a similar operation in say MS Access report. Is there a better proportional
resizing function available that can maintain better image quality?
Many Thanks
Regards
01.public Bitmap ProportionallyResizeBitmap(Bitmap src, int maxWidth, int
maxHeight)
02.{
03. // original dimensions
04. int w = src.Width;
05. int h = src.Height;
06. // Longest and shortest dimension
07. int longestDimension = (w > h) ? w : h;
08. int shortestDimension = (w < h) ? w : h;
09. // propotionality
10. float factor = (float)longestDimension / shortestDimension;
11. // default width is greater than height
12. double newWidth = maxWidth;
13. double newHeight = maxWidth / factor;
14. // if height greater than width recalculate
15. if (w < h) {
16. newWidth = maxHeight / factor;
17. newHeight = maxHeight;
18. }
19. // Create new Bitmap at new dimensions
20. Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
21. using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
{
22. g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
23. }
24. return result;
25.}