Advertisement
rozman50

Untitled

Jul 17th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. private async Task<string> saveImage(string destinationPath, IFormFile file, string fileName = null)
  2. {
  3. var name = string.Empty;
  4. var destinationPathWithName = string.Empty;
  5. if (file.Length > 0)
  6. {
  7. if(fileName != null)
  8. name = fileName;
  9. else
  10. name = file.FileName;
  11.  
  12. destinationPathWithName = Path.Combine(destinationPath, name);
  13. try
  14. {
  15. using (var stream = new FileStream(destinationPathWithName, FileMode.Create))
  16. {
  17. await file.CopyToAsync(stream);
  18. }
  19. }
  20. catch (Exception e)
  21. {
  22. var msg = e.Message;
  23. }
  24. }
  25. return destinationPathWithName;
  26. }
  27.  
  28. public string ConvertImageToJpg(string sourceImagePath, string destinationImagePath)
  29. {
  30.  
  31. var fileInfo = new FileInfo(sourceImagePath);
  32.  
  33. if (fileInfo.Length > 1)
  34. {
  35.  
  36. var directory = Path.GetDirectoryName(destinationImagePath);
  37.  
  38. if (!Directory.Exists(directory))
  39. {
  40. Directory.CreateDirectory(directory);
  41. }
  42.  
  43.  
  44. using (MagickImage image = new MagickImage(sourceImagePath))
  45. {
  46.  
  47.  
  48. destinationImagePath = Path.Combine(directory, Path.GetFileNameWithoutExtension(destinationImagePath) + ".jpg");
  49. image.BackgroundColor = MagickColors.White;
  50. image.Alpha(AlphaOption.Remove);
  51. image.Format = MagickFormat.Jpg;
  52. image.Quality = 90;
  53. image.Write(destinationImagePath);
  54.  
  55. }
  56.  
  57. }
  58. return destinationImagePath;
  59. }
  60.  
  61. public string ConvertImageToJpg(string sourceImagePath, string destinationImagePath, int width, int height)
  62. {
  63.  
  64. var fileInfo = new FileInfo(sourceImagePath);
  65.  
  66. if (fileInfo.Length > 1)
  67. {
  68.  
  69. var directory = Path.GetDirectoryName(destinationImagePath);
  70.  
  71. if (!Directory.Exists(directory))
  72. {
  73. Directory.CreateDirectory(directory);
  74. }
  75.  
  76. using (MagickImage image = new MagickImage(sourceImagePath))
  77. {
  78.  
  79.  
  80. destinationImagePath = Path.Combine(directory, Path.GetFileNameWithoutExtension(destinationImagePath) + ".jpg");
  81. image.Alpha(AlphaOption.Remove);
  82. image.Resize(width, height);
  83. image.Format = MagickFormat.Jpg;
  84. image.Quality = 90;
  85. image.Write(destinationImagePath);
  86. }
  87.  
  88. }
  89. return destinationImagePath;
  90. }
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement