Proportional resizing of an image

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.}
 
G

Gregory A. Beamer

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?

One thing that makes development hard is different systems protect a
developer from himself at different levels. Many of the Office products
will allow bad practices and cover for them. The same is true for bad
HTML in older IE browsers (and to an extent, the newer versions). This
"covering" gets people into bad habits they do not even realize are bad
habits. Then you come to a system that does not provide the same level
of "cover" and things begin to puke.

I would ensure the original image is large enough to be upsized. If not,
you will always get some artifacts. I am not sure what Access does when
you resize in a report, but it may upsize the actual image, which could
potentially reduce the artifacts present. Even with Access, I would
ensure the image is size properly, however.

If this does not solve the problem, you may have to actually size the
image to the size it appears on the form. Then you will have a pixel for
pixel representation. You can use GDI+ for this, but realize its
algorithms are not the best with some image types.

Peace and Grace,
Greg

--
Vote for Miranda's Christmas Story
http://tinyurl.com/mirandabelieve

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
P

Peter Duniho

John said:
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?

Depends on the original size and the new size, of which you are not
specific.

Patrice's reply should be helpful. If not, there have been a large
number of discussions in this newsgroup regarding the question of image
resizing and quality of the operation. If you need additional details,
a search in Google Groups should quickly find the information you need.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top