locked
Image rotate RRS feed

  • Question

  • User1587254688 posted
    I want to rotate image and save it but it gives an error like this; GDI+ generic error... Can you help? Thanks.
    1    protected void Page_Load(object sender, EventArgs e)
    2        {
    3            string yol = Server.MapPath("a.jpg");
    4            System.Drawing.Image resim = System.Drawing.Image.FromFile(yol);
    5            resim = cevir((Bitmap)resim, 270);
    6            resim.Save(yol);
    7            resim.Dispose();
    8            resim = null;
    9        }
    10   
    11       public static Bitmap cevir(Bitmap b, float angle)
    12       {
    13           //create a new empty bitmap to hold rotated image
    14           Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
    15           //make a graphics object from the empty bitmap
    16           Graphics g = Graphics.FromImage(returnBitmap);
    17           //move rotation point to center of image
    18           g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
    19           //rotate
    20           g.RotateTransform(angle);
    21           //move image back
    22           g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
    23           //draw passed in image onto graphics object
    24           g.DrawImage(b, new Point(0, 0));
    25           g.Dispose();
    26   
    27           return returnBitmap;
    28       }
    
     

     

    Friday, January 2, 2009 9:23 AM

Answers

  • User1587254688 posted

      Now it's working with oku function. But it doesn't compress the jpeg file. How can I compress it?

     
    1    using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Web;
    5 using System.Web.UI;
    6 using System.Web.UI.WebControls;
    7 using System.Drawing;
    8 using System.IO;
    9
    10 public partial class _Default : System.Web.UI.Page
    11 {
    12 public static System.Drawing.Image oku(string fileName)
    13 {
    14 System.Drawing.Image theImage = null;
    15 using (FileStream fileStream = new FileStream(fileName, FileMode.Open,
    16 FileAccess.Read))
    17 {
    18 byte[] img;
    19 img = new byte[fileStream.Length];
    20 fileStream.Read(img, 0, img.Length);
    21 fileStream.Close();
    22 theImage = System.Drawing.Image.FromStream(new MemoryStream(img));
    23 img = null;
    24 }
    25 GC.Collect();
    26 return theImage;
    27 }
    28
    29 protected void Page_Load(object sender, EventArgs e)
    30 {
    31 string yol = Server.MapPath("a.jpg");
    32 System.Drawing.Image resim = oku(Server.MapPath("a.jpg")); ;
    33 resim.RotateFlip(RotateFlipType.Rotate90FlipNone);
    34 resim.Save(Server.MapPath("a.jpg"));
    35 resim.Dispose();
    36 resim = null;
    37 }
    38 }
    39
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2009 1:10 PM

All replies

  • User187056398 posted

    From the online help on FromImage:

        The file remains locked until the Image is disposed.

    You are never disposing the original image.  You can save to a new file name, or create separate objects and dispose the original before saving the new one.

     

    Friday, January 2, 2009 9:58 AM
  • User1587254688 posted
    I've changed it but it doesn't works [:(] Is this code correct?
    1    string yol = Server.MapPath("a.jpg");
    2            System.Drawing.Image resim = System.Drawing.Image.FromFile(yol);
    3            resim = cevir((Bitmap)resim, 270);
    4            resim.Save(Server.MapPath("b.jpg"));
    5            resim.Dispose();
    6            resim = null;
    7            File.Delete(Server.MapPath("a.jpg"));
    8            File.Move(Server.MapPath("b.jpg"), Server.MapPath("a.jpg"));
    
     

     

    Friday, January 2, 2009 10:17 AM
  • User187056398 posted

    No.  In these lines

    2            System.Drawing.Image resim = System.Drawing.Image.FromFile(yol);
    3            resim = cevir((Bitmap)resim, 270);

    You have a reference to an Image called resim that is lurking in memory.

    Then you re-assign the reference to a new image returned from your function.  This 'abandons' the first image.  Eventually something will time out and the resource will be released but it's not want you want.

     

    Friday, January 2, 2009 11:12 AM
  • User1587254688 posted

     So, how can I release the fist image? Or is there another way like working on memory?

    Friday, January 2, 2009 12:23 PM
  • User1587254688 posted

      Now it's working with oku function. But it doesn't compress the jpeg file. How can I compress it?

     
    1    using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Web;
    5 using System.Web.UI;
    6 using System.Web.UI.WebControls;
    7 using System.Drawing;
    8 using System.IO;
    9
    10 public partial class _Default : System.Web.UI.Page
    11 {
    12 public static System.Drawing.Image oku(string fileName)
    13 {
    14 System.Drawing.Image theImage = null;
    15 using (FileStream fileStream = new FileStream(fileName, FileMode.Open,
    16 FileAccess.Read))
    17 {
    18 byte[] img;
    19 img = new byte[fileStream.Length];
    20 fileStream.Read(img, 0, img.Length);
    21 fileStream.Close();
    22 theImage = System.Drawing.Image.FromStream(new MemoryStream(img));
    23 img = null;
    24 }
    25 GC.Collect();
    26 return theImage;
    27 }
    28
    29 protected void Page_Load(object sender, EventArgs e)
    30 {
    31 string yol = Server.MapPath("a.jpg");
    32 System.Drawing.Image resim = oku(Server.MapPath("a.jpg")); ;
    33 resim.RotateFlip(RotateFlipType.Rotate90FlipNone);
    34 resim.Save(Server.MapPath("a.jpg"));
    35 resim.Dispose();
    36 resim = null;
    37 }
    38 }
    39
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2009 1:10 PM
  • User187056398 posted

    But it doesn't compress the jpeg file. How can I compress it?

    Here's a sample: http://www.vbforums.com/showthread.php?t=343060

     

    Friday, January 2, 2009 3:57 PM
  • User187056398 posted

    By the way,  you may find this technique helpful...

            String ImagePath = MapPath(@"~\Images\April2.jpg");

            System.Drawing.Image MyImage = System.Drawing.Image.FromFile(ImagePath);

            MyImage.RotateFlip(RotateFlipType.Rotate270FlipX);
            MyImage.Save(ImagePath);

     
    Friday, January 2, 2009 6:45 PM