0

I have upgraded my project from .net framework to .net 6 (core). In my project, there are many places where Bitmap is used. I have read in the microsoft documentations that System.Drawing.Common will only support the Windows platform and even after adding the EnableUnixSupport configuration, it will not be supported in net7.So, now I am using ImageSharp.Web. I have the scenario where I save a file as Image (the format is .tiff) then I read from that path as bitmap and save as PNG ( due to some business rule) Following is the line of code I am trying change:

Bitmap.FromFile(completePath).Save(pngPath, ImageFormat.Png);

This is the code I have converted into. The only issue is how to save as a new file name as the Tiff file has tiff in the file name.

string extension = _GetExtension(img.ContentType);
       


 if (extension == Constants.TiffExtension)
            {
           
            fileName = fileName.Replace(Constants.TiffExtension, "PNG");
           
            using (var outputStream = new FileStream(completePath, FileMode.CreateNew))
            {
                var image = SixLabors.ImageSharp.Image.Load(completePath);

                image.SaveAsync(outputStream, new PngEncoder()); //how to save new file name?
            }
           
        }
Sana Ahmed
  • 462
  • 3
  • 25

2 Answers2

1

You can use the image.Save(fileName); overload to save a image to a file. The file name overload that takes just a path will automatically choose the correct encoder based on the file extension.

tocsoft
  • 1,709
  • 16
  • 22
  • Cannot find any such overload in the version 2.0.1 of imageSharp.web – Sana Ahmed Jun 12 '22 at 01:18
  • 1
    That's because ImageSharp.Web is ASP.NET Core middleware. The extension you are looking for is documented under ImageSharp. https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.ImageExtensions.html#SixLabors_ImageSharp_ImageExtensions_Save_SixLabors_ImageSharp_Image_System_String_SixLabors_ImageSharp_Formats_IImageEncoder_ – James South Jun 13 '22 at 03:00
1

I was using the ImageSharp.Web package while the one I needed was the basic ImageSharp package. Special thanks to @James South for correcting me and @tocsoft for the guidance.

I have fixed it by the following code which is working:

 if (extension == Constants.Conversion.TiffExtension)
            {
                using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(completePath))
                {
                    string pngPath = completePath.Replace(Constants.Conversion.TiffExtension, Conversion.DefaultExtension);
                    image.Save(pngPath);
                    fileName = fileName.Replace(Constants.Conversion.TiffExtension, Conversion.DefaultExtension);
                }
            } 
Sana Ahmed
  • 462
  • 3
  • 25