Best Practice in Storing images

Vuyiswa Maseko 351 Reputation points
2021-04-01T17:09:16.393+00:00

Good Day Everyone

I have a Xamarin app where i request a user to store images , i stop the bath on the DB and Store the Images on the File system and dynamically generate a url and that works perfectly well. and the files get loaded fast especially with the FFLoading cache plugin.

So what i do if a user upload the images, i call a function that will resize the image. So for an Image i create two images a thumbnail image and also a normal size image. So i realized that my method of resizing the images is problematic because some images are cut off

My function create a Thumbnail of 150 x 150 px

and other images it create them at 350 x350 px

using this function i create 350 x 350 images , as you can see i use MagickImage which has an ability to resize the image. https://github.com/dlemstra/Magick.NET/blob/main/docs/ConvertImage.md

  public static void ResizeToFixedSize(byte[] filebytes, string destfilename, int width, int height)
    {
        // Read from file
        using (var image = new MagickImage(filebytes))
        {
            var size = new MagickGeometry(width, height);
            // This will resize the image to a fixed size without maintaining the aspect ratio.
            // Normally an image will be resized to fit inside the specified size.
            size.IgnoreAspectRatio = true;

            image.Resize(size);

            // Save the result
            image.Write(destfilename);
        }
    }

using this function i create Thumbnails

public Bitmap CreateThumbnail(Bitmap loBMP, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;

            //*** If the image is smaller than a thumbnail just return it
            if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                return loBMP;

            if (loBMP.Width > loBMP.Height)
            {
                lnRatio = (decimal)lnWidth / loBMP.Width;
                lnNewWidth = lnWidth;
                decimal lnTemp = loBMP.Height * lnRatio;
                lnNewHeight = (int)lnTemp;
            }
            else
            {
                lnRatio = (decimal)lnHeight / loBMP.Height;
                lnNewHeight = lnHeight;
                decimal lnTemp = loBMP.Width * lnRatio;
                lnNewWidth = (int)lnTemp;
            }
            bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
            Graphics g = Graphics.FromImage(bmpOut);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
            g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

            loBMP.Dispose();
        }
        catch
        {
            return null;
        }

        return bmpOut;
    }

the other reason im changing the size of the images , is that i want to reduce the file size so that when its loaded it does not take time.

Any suggestion is welcomed no matter how small it is , to make the process better and effecient

Thanks

Developer technologies | .NET | Xamarin
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.