Sunday, April 11, 2010

Image resize code in c#

public byte[] imagereduce(System.Drawing.Image imgInput, int maxImageSize)
    {
        int tWidth;
        int tHeight;
        double widthHeightRatio = (double)imgInput.Width / (double)imgInput.Height;
        // If width greater than height, then width should be max image size, otherwise height should be.
        // Image should keep the same proportions.
        //tWidth = maxImageSize;
        //tHeight = (int)(maxImageSize / widthHeightRatio);
        if (widthHeightRatio > 1.0)
        {
            tWidth = maxImageSize;
            tHeight = (int)(maxImageSize / widthHeightRatio);
        }
        else
        {
            tWidth = (int)(maxImageSize * widthHeightRatio);
            tHeight = maxImageSize;
        }
        //Session["imgheight"] = tHeight;
        //Session["imgwidth"] = tWidth;
        System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        System.Drawing.Image myThumbnail = imgInput.GetThumbnailImage(tWidth, tHeight, myCallBack, IntPtr.Zero);
        //System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(myThumbnail);

        //gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, tWidth, (int)tHeight);

        //System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];

        ////Set the parameters for defining the quality of the thumbnail... here it is set to 100%
        //System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
        //eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

        //gr.DrawImage(myThumbnail, rectDestination, 0, 0, tWidth, tHeight, System.Drawing.GraphicsUnit.Pixel);
        ////myThumbnail.Save(Server.MapPath(sSavePath + sThumbFile));
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        //myThumbnail.Save(ms, codec,eParams);
        myThumbnail.Save(ms, ImageFormat.Jpeg);
        byte[] bitmapData = ms.ToArray();
        return bitmapData;
    }
    public bool ThumbnailCallback()
    {
        return false;
    }
    public bool checkext(string ext)
    {
        ext = ext.ToLower();
        if (ext == ".jpg"){return true;}
        else if (ext == ".bmp"){ return true;}
        else if (ext == ".gif"){ return true;}
        else if (ext == "jpg") { return true;}
        else if (ext == ".png")
        {
            return true;
        }
        else if (ext == ".jpeg")
        {
            return true;
        }
        else if (ext == "png")
        {
            return true;
        }
        else if (ext == "bmp")
        {
            return true;
        }
        else if (ext == "gif")
        {
            return true;
        }
        else if (ext == "jpeg")
        {
            return true;
        }
        else
        {
            return false;
        }

    }

--
www.cinehour.com