Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. private void cameraTask_Completed(object sender, PhotoResult e)
  2. {
  3. if (e.TaskResult == TaskResult.OK)
  4. {
  5. BitmapImage bmi = new BitmapImage();
  6. bmi.SetSource(e.ChosenPhoto);
  7. //MessageBox.Show(bmi.PixelWidth.ToString() + "x" + bmi.PixelHeight.ToString());
  8.  
  9. var gcd = GCD(bmi.PixelWidth, bmi.PixelHeight);
  10. var result = string.Format("{0}:{1}", bmi.PixelWidth / gcd, bmi.PixelHeight / gcd);
  11.  
  12. WriteableBitmap wb;
  13. Stream stream;
  14.  
  15. switch (result)
  16. {
  17. case "3:4":
  18. wb = new WriteableBitmap(480,640);
  19. break;
  20. case "4:3":
  21. wb = new WriteableBitmap(640,480);
  22. break;
  23. case "9:16":
  24. wb = new WriteableBitmap(448, 800);
  25. break;
  26. case "16:9":
  27. wb = new WriteableBitmap(800, 448);
  28. break;
  29. default:
  30. wb = null;
  31. return;
  32. }
  33. //Set the wb to the original stream?
  34. wb.SetSource(e.ChosenPhoto);
  35.  
  36. //Convert the wb to a stream for saving
  37. stream = new MemoryStream(wb.ToByteArray());
  38.  
  39. //Need to replace the following line with the new image stream for saving?
  40. //var capturedPicture = new CapturedPicture(e.OriginalFileName, e.ChosenPhoto);
  41. var capturedPicture = new CapturedPicture(e.OriginalFileName, stream);
  42.  
  43. }
  44. }
  45.  
  46. public int GCD(int a, int b)
  47. {
  48. while (a != 0 && b != 0)
  49. {
  50. if (a > b)
  51. a %= b;
  52. else
  53. b %= a;
  54. }
  55. if (a == 0)
  56. return b;
  57. else
  58. return a;
  59. }
  60.  
  61. private void cameraTask_Completed(object sender, PhotoResult e)
  62. {
  63. if (e.TaskResult == TaskResult.OK)
  64. {
  65. BitmapImage bmi = new BitmapImage();
  66. bmi.SetSource(e.ChosenPhoto);
  67.  
  68. var gcd = GCD(bmi.PixelWidth, bmi.PixelHeight);
  69. var result = string.Format("{0}:{1}", bmi.PixelWidth / gcd, bmi.PixelHeight / gcd);
  70.  
  71. WriteableBitmap wb = new WriteableBitmap(bmi);
  72. Stream stream = new MemoryStream();
  73.  
  74. switch (result)
  75. {
  76. case "3:4":
  77. wb.SaveJpeg(stream, 480, 640, 0, 100);
  78. break;
  79. case "4:3":
  80. wb.SaveJpeg(stream, 640, 480, 0, 100);
  81. break;
  82. case "9:16":
  83. wb.SaveJpeg(stream, 448, 800, 0, 100);
  84. break;
  85. case "16:9":
  86. wb.SaveJpeg(stream, 800, 448, 0, 100);
  87. break;
  88. default:
  89. wb = null;
  90. return;
  91. }
  92.  
  93. stream.Seek(0, SeekOrigin.Begin);
  94.  
  95. //var capturedPicture = new CapturedPicture(e.OriginalFileName, e.ChosenPhoto);
  96. var capturedPicture = new CapturedPicture(e.OriginalFileName, stream);
  97.  
  98. public static Image resizeImage(Image imgToResize, Size size)
  99. {
  100. return (Image)(new Bitmap(imgToResize, size));
  101. }
  102.  
  103. yourImage = resizeImage(yourImage, new Size(50,50));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement