locked
Rotating Image RRS feed

  • Question

  • User-541003552 posted

    I use the code below to retrieve images from database and display in image. How can i rotate the image lets say 180 degrees. Tried all examples. Can anyone help. Code below

    Thanks

                SqlConnection conn = new SqlConnection(strconnection);
                SqlDataAdapter sda = new SqlDataAdapter("select Front_Image, from Transactions WHERE ItemIdentifier = '" + Request.QueryString["ItemIdentifier"] + "'", conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
    
                foreach (DataRow row in dt.Rows)
                    {
                    //Get the byte array from image file
                    byte[] imgBytes = (byte[])row["Front_Image"];
    
                    string imgString = Convert.ToBase64String(imgBytes);
    
                    Front_img.Src = String.Format("data:image/Bmp;base64,{0}\"", imgString);
    }

    Wednesday, April 8, 2015 8:22 AM

Answers

  • User753101303 posted

    Have you tried http://forums.asp.net/t/2044291.aspx?Rotating+Images and what happens? (try to keep discussing in a single thread rather than opening a new thread with the same question).

    If you want to use the non CSS soltuion at some point it likely needs to be loaded as an image object (System.Drawing or 3rd party Library) as rotating an image is likely not just the matter of doing some simple processing on the byte array you get...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 8, 2015 8:28 AM
  • User-657100335 posted

    From your code, I came up with something like:

    byte[] imgBytes = (byte[])row["Front_Image"];
    
    //byte array to memory stream
    using (MemoryStream ms = new MemoryStream(imgBytes))
    {
       //create the image from memory stream
       Image test_image = Image.FromStream(ms);
       
       //rotate the image 180 degrees
       test_image.RotateFlip(RotateFlipType.Rotate180FlipNone);
       
       //convert the rotated image back to byte array
       ImageConverter _imageConverter = new ImageConverter();
       byte[] xByte = (byte[])_imageConverter.ConvertTo(test_image, typeof(byte[]));
    
    
       string imgString = Convert.ToBase64String(xByte);

       
    Front_img.Src = String.Format("data:image/Bmp;base64,{0}\"", imgString);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2015 3:07 AM
  • User-1078840699 posted

    Hello,

    Here a method you can use to rotate an image in C#:

    /// <summary>
    /// method to rotate an image either clockwise or counter-clockwise
    /// </summary>
    /// <param name="img">the image to be rotated</param>
    /// <param name="rotationAngle">the angle (in degrees).
    /// NOTE: 
    /// Positive values will rotate clockwise
    /// negative values will rotate counter-clockwise
    /// </param>
    /// <returns></returns>
    public static Image RotateImage(Image img, float rotationAngle)
    {
        //create an empty Bitmap image
        Bitmap bmp = new Bitmap(img.Width, img.Height);
    
        //turn the Bitmap into a Graphics object
        Graphics gfx = Graphics.FromImage(bmp);
    
        //now we set the rotation point to the center of our image
        gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
    
        //now rotate the image
        gfx.RotateTransform(rotationAngle);
    
        gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
    
        //set the InterpolationMode to HighQualityBicubic so to ensure a high
        //quality image once it is transformed to the specified size
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
        //now draw our new image onto the graphics object
        gfx.DrawImage(img, new Point(0, 0));
    
        //dispose of our Graphics object
        gfx.Dispose();
    
        //return the image
        return bmp;
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2015 4:21 PM

All replies

  • User753101303 posted

    Have you tried http://forums.asp.net/t/2044291.aspx?Rotating+Images and what happens? (try to keep discussing in a single thread rather than opening a new thread with the same question).

    If you want to use the non CSS soltuion at some point it likely needs to be loaded as an image object (System.Drawing or 3rd party Library) as rotating an image is likely not just the matter of doing some simple processing on the byte array you get...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 8, 2015 8:28 AM
  • User2008642861 posted

    Hi KeeEdwins,

    According to your requirement, I suggest that you could achieve your goal by using RotateFlip Method or telerikUI. Please check below example.

    http://www.c-sharpcorner.com/uploadfile/hrojasara/how-to-rotate-images-in-Asp-Net/

    http://demos.telerik.com/aspnet-ajax/imageeditor/examples/rotate/defaultcs.aspx

    The first link is server-side method RotateFilp method, and the second link is client-side teleriUI. I suggest that you could choose one according to your demands.

    Hope this could be helpful to you.

    Best regards,

    Archer

    Thursday, April 9, 2015 1:34 AM
  • User-657100335 posted

    From your code, I came up with something like:

    byte[] imgBytes = (byte[])row["Front_Image"];
    
    //byte array to memory stream
    using (MemoryStream ms = new MemoryStream(imgBytes))
    {
       //create the image from memory stream
       Image test_image = Image.FromStream(ms);
       
       //rotate the image 180 degrees
       test_image.RotateFlip(RotateFlipType.Rotate180FlipNone);
       
       //convert the rotated image back to byte array
       ImageConverter _imageConverter = new ImageConverter();
       byte[] xByte = (byte[])_imageConverter.ConvertTo(test_image, typeof(byte[]));
    
    
       string imgString = Convert.ToBase64String(xByte);

       
    Front_img.Src = String.Format("data:image/Bmp;base64,{0}\"", imgString);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2015 3:07 AM
  • User-1078840699 posted

    Hello,

    Here a method you can use to rotate an image in C#:

    /// <summary>
    /// method to rotate an image either clockwise or counter-clockwise
    /// </summary>
    /// <param name="img">the image to be rotated</param>
    /// <param name="rotationAngle">the angle (in degrees).
    /// NOTE: 
    /// Positive values will rotate clockwise
    /// negative values will rotate counter-clockwise
    /// </param>
    /// <returns></returns>
    public static Image RotateImage(Image img, float rotationAngle)
    {
        //create an empty Bitmap image
        Bitmap bmp = new Bitmap(img.Width, img.Height);
    
        //turn the Bitmap into a Graphics object
        Graphics gfx = Graphics.FromImage(bmp);
    
        //now we set the rotation point to the center of our image
        gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
    
        //now rotate the image
        gfx.RotateTransform(rotationAngle);
    
        gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
    
        //set the InterpolationMode to HighQualityBicubic so to ensure a high
        //quality image once it is transformed to the specified size
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
        //now draw our new image onto the graphics object
        gfx.DrawImage(img, new Point(0, 0));
    
        //dispose of our Graphics object
        gfx.Dispose();
    
        //return the image
        return bmp;
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2015 4:21 PM