Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private async Task<string> saveImage(string destinationPath, IFormFile file, string fileName = null)
- {
- var name = string.Empty;
- var destinationPathWithName = string.Empty;
- if (file.Length > 0)
- {
- if(fileName != null)
- name = fileName;
- else
- name = file.FileName;
- destinationPathWithName = Path.Combine(destinationPath, name);
- try
- {
- using (var stream = new FileStream(destinationPathWithName, FileMode.Create))
- {
- await file.CopyToAsync(stream);
- }
- }
- catch (Exception e)
- {
- var msg = e.Message;
- }
- }
- return destinationPathWithName;
- }
- public string ConvertImageToJpg(string sourceImagePath, string destinationImagePath)
- {
- var fileInfo = new FileInfo(sourceImagePath);
- if (fileInfo.Length > 1)
- {
- var directory = Path.GetDirectoryName(destinationImagePath);
- if (!Directory.Exists(directory))
- {
- Directory.CreateDirectory(directory);
- }
- using (MagickImage image = new MagickImage(sourceImagePath))
- {
- destinationImagePath = Path.Combine(directory, Path.GetFileNameWithoutExtension(destinationImagePath) + ".jpg");
- image.BackgroundColor = MagickColors.White;
- image.Alpha(AlphaOption.Remove);
- image.Format = MagickFormat.Jpg;
- image.Quality = 90;
- image.Write(destinationImagePath);
- }
- }
- return destinationImagePath;
- }
- public string ConvertImageToJpg(string sourceImagePath, string destinationImagePath, int width, int height)
- {
- var fileInfo = new FileInfo(sourceImagePath);
- if (fileInfo.Length > 1)
- {
- var directory = Path.GetDirectoryName(destinationImagePath);
- if (!Directory.Exists(directory))
- {
- Directory.CreateDirectory(directory);
- }
- using (MagickImage image = new MagickImage(sourceImagePath))
- {
- destinationImagePath = Path.Combine(directory, Path.GetFileNameWithoutExtension(destinationImagePath) + ".jpg");
- image.Alpha(AlphaOption.Remove);
- image.Resize(width, height);
- image.Format = MagickFormat.Jpg;
- image.Quality = 90;
- image.Write(destinationImagePath);
- }
- }
- return destinationImagePath;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement