Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. Bitmap original,resizedImage;
  2. try
  3. {
  4.  
  5. using (FileStream fs = new System.IO.FileStream(imageLabel.Text, System.IO.FileMode.Open))
  6. {
  7.  
  8. original = new Bitmap(fs);
  9. }
  10.  
  11. int rectHeight = BOXWIDTH;
  12. int rectWidth = BOXWIDTH;
  13. //if the image is squared set it's height and width to the smallest of the desired dimensions (our box). In the current example rectHeight<rectWidth
  14. if (original.Height == original.Width)
  15. {
  16. resizedImage = new Bitmap(original, rectHeight, rectHeight);
  17. }
  18. else
  19. {
  20. //calculate aspect ratio
  21. float aspect = original.Width / (float)original.Height;
  22. int newWidth, newHeight;
  23. //calculate new dimensions based on aspect ratio
  24. newWidth = (int)(rectWidth * aspect);
  25. newHeight = (int)(newWidth / aspect);
  26. //if one of the two dimensions exceed the box dimensions
  27. if (newWidth > rectWidth || newHeight > rectHeight)
  28. {
  29. //depending on which of the two exceeds the box dimensions set it as the box dimension and calculate the other one based on the aspect ratio
  30. if (newWidth > newHeight)
  31. {
  32. newWidth = rectWidth;
  33. newHeight = (int)(newWidth / aspect);
  34.  
  35. }
  36. else
  37. {
  38. newHeight = rectHeight;
  39. newWidth = (int)(newHeight * aspect);
  40.  
  41. }
  42. }
  43. resizedImage = new Bitmap(original, newWidth, newHeight);
  44.  
  45.  
  46.  
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. MessageBox.Show( ex.Message);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement