Solved

C# Image Resizing Code to explain

Posted on 2006-10-23
2
963 Views
Last Modified: 2008-01-09
I was given this code to try out for resizing images.

can someone tell me how to call this. I know it is a newbE question but I believe I can just type
ResizeFromByteArray("c:\\img\\filename.jpg", "500",Byte[] byteArrayIn);

but I have no idea what I put in for the
Byte[] byteArrayIn
part of the code does any one here know?



 public static byte[] ResizeFromByteArray(string fileName, int MaxSideSize, Byte[] byteArrayIn)
        {
            byte[] byteArray = null;  // really make this an error gif
            MemoryStream ms = new MemoryStream(byteArrayIn);
            byteArray = CodeCampSV.Utils.ResizeFromStream(fileName, MaxSideSize, ms);



            return byteArray;
        }

        /// <summary>
        /// converts from input stream to output bytearray
        /// inspired from: http://www.eggheadcafe.com/articles/20030515.asp
        /// </summary>
        /// <param name="ImageSavePath"></param>
        /// <param name="MaxSideSize"></param>
        /// <param name="Buffer"></param>
        /// <returns></returns>
        public static byte[] ResizeFromStream(string fileName, int MaxSideSize, Stream Buffer)
        {
            byte[] byteArray = null;  // really make this an error gif

            try
            {

               


                Bitmap bitMap = new Bitmap(Buffer);
                int intOldWidth = bitMap.Width;
                int intOldHeight = bitMap.Height;

                int intNewWidth;
                int intNewHeight;

                int intMaxSide;

                if (intOldWidth >= intOldHeight)
                {
                    intMaxSide = intOldWidth;
                }
                else
                {
                    intMaxSide = intOldHeight;
                }

                if (intMaxSide > MaxSideSize)
                {
                    //set new width and height
                    double dblCoef = MaxSideSize / (double)intMaxSide;
                    intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
                    intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
                }
                else
                {
                    intNewWidth = intOldWidth;
                    intNewHeight = intOldHeight;
                }

                Size ThumbNailSize = new Size(intNewWidth, intNewHeight);
                System.Drawing.Image oImg = System.Drawing.Image.FromStream(Buffer);
                System.Drawing.Image oThumbNail = new Bitmap
                    (ThumbNailSize.Width, ThumbNailSize.Height);
                Graphics oGraphic = Graphics.FromImage(oThumbNail);
                oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Rectangle oRectangle = new Rectangle
                    (0, 0, ThumbNailSize.Width, ThumbNailSize.Height);

                oGraphic.DrawImage(oImg, oRectangle);

                //string fileName = Context.Server.MapPath("~/App_Data/") + "test1.jpg";
                //oThumbNail.Save(fileName, ImageFormat.Jpeg);
                MemoryStream ms = new MemoryStream();
                oThumbNail.Save(ms, ImageFormat.Jpeg);
                byteArray = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

                oGraphic.Dispose();
                oImg.Dispose();
                ms.Close();
                ms.Dispose();
            }
            catch (Exception)
            {
                int newSize = MaxSideSize - 20;
                Bitmap bitMap = new Bitmap(newSize, newSize);
                Graphics g = Graphics.FromImage(bitMap);
                g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, newSize, newSize));

                Font font = new Font("Courier", 8);
                SolidBrush solidBrush = new SolidBrush(Color.Red);
                g.DrawString("Failed File", font, solidBrush, 10, 5);
                g.DrawString(fileName, font, solidBrush, 10, 50);

                MemoryStream ms = new MemoryStream();
                bitMap.Save(ms, ImageFormat.Jpeg);
                byteArray = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

                ms.Close();
                ms.Dispose();
                bitMap.Dispose();
                solidBrush.Dispose();
                g.Dispose();
                font.Dispose();

            }
            return byteArray;
        }

0
Comment
Question by:Scripter25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Expert Comment

by:saleek
ID: 17794270
Hi,

I believe that this code is to convert an image into a bytearray for transport through a webservice.

If you just want to resize an image then you can use the following code:

http://west-wind.com/weblog/posts/283.aspx

Alternatively, do a search for "ASP.NET Resize image" and you will find many examples.

regards,

KS

0
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 17794868
You can also call ResizeFromStream() method if that is easier.  Given a filename, here is how you would call ResizeFromStream
   string fileName=base.MapPath("~/images/largePhoto.jpg";
   FileStream inFile=System.IO.File.Open (fileName, FileMode.Open);

   byte[] results = CodeCampSV.Utils.ResizeFromStream(base.MapPath("~/images/SmallPhoto.jpg", 10, inFile);

You would call the other method if you already had the image in memory (i.e. you just created it).  If you called the ResizeFromByteArray() method for a file, first you would have to read the file into a byte array, then the function would just convert the byte array  into a Stream and call ResizeFromStream().  So in your process would then have the file in memory at least twice (once in the byte array & once in the memory stream).
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question