ImageEngine.CreateThumbnail C# (CSharp) Method

CreateThumbnail() public method

public CreateThumbnail ( byte byteArrayIn ) : Bitmap
byteArrayIn byte
return Bitmap
    public Bitmap CreateThumbnail(byte[] byteArrayIn)
    {
        System.Drawing.Bitmap bmpOut = null;

        try
        {
            //  Only store the image if its size is greater than 1KB.
            //  if (imageData.Length > 1024 && imageData.Length < 15000)
            if (byteArrayIn != null && byteArrayIn.Length > 2048)
            {

                //  Bitmap loBMP = (Bitmap)ByteArrayToImage(byteArrayIn);

                //  using (MemoryStream ms = new MemoryStream(byteArrayIn))
                {
                    MemoryStream ms = new MemoryStream(byteArrayIn);
                    Bitmap loBMP = (Bitmap)System.Drawing.Image.FromStream(ms);

                    if (loBMP != null)
                    {
                        ImageFormat loFormat = loBMP.RawFormat;

                        //  The image has to be atleast somewhat big (Done so as to avoid icons.)
                        if (loBMP.PhysicalDimension.Height < 200 && loBMP.PhysicalDimension.Width < 200)
                            return null;

                        if (loBMP.Height < 150 || loBMP.Width < 150)
                        {
                            if ((loBMP.Height < loBMP.Width / 3) || (loBMP.Width < loBMP.Height / 3))
                            {
                                return null;
                            }
                        }

                        if ((loBMP.Height < loBMP.Width / 2) || (loBMP.Width < loBMP.Height / 2))
                        {
                            return null;
                        }

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

                        if (loBMP.Width.Equals(loBMP.Height))
                            return null;

                        ////  Don't think I need this kind of scaling, I just need an exact square image (100 X 100)
                        //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;
                        //}

                        // System.Drawing.Image imgOut = loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight, null,IntPtr.Zero);

                        // This code creates cleaner (though bigger) thumbnails and properly and handles GIF files better by generating a white background for transparent images (as opposed to black)

                        bmpOut = new Bitmap(loBMP.Width, loBMP.Height);
                        Graphics g = Graphics.FromImage(bmpOut);
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        g.FillRectangle(Brushes.White, 0, 0, loBMP.Width, loBMP.Height);
                        g.DrawImage(loBMP, 0, 0, loBMP.Width, loBMP.Height);

                        g.Dispose();
                        loBMP.Dispose();
                    }
                    else
                    {
                        bmpOut = null;
                    }
                }
            }
            else
            {
                bmpOut = null;
            }
        }
        catch (Exception ex)
        {
            bmpOut = null;
        }
        return bmpOut;
    }

Same methods

ImageEngine::CreateThumbnail ( byte byteArrayIn, int lnWidth, int lnHeight ) : Bitmap
ImageEngine::CreateThumbnail ( string lcFilename, int lnWidth, int lnHeight ) : Bitmap