Avinash patil
Avinash patil

Reputation: 1799

how to rotate image in File in C# & WPF application

I have WPF and C# application. which captures the images and Save in to file(*.jpg). I have the image path and i want to rotate image saved in File through the c# code. and Save the Rotated image in same file.

How can i do that?

Upvotes: 3

Views: 2914

Answers (2)

mahdi e
mahdi e

Reputation: 43

you can use my method:

    BitmapImage rotateImage(string filename,int angle)
    {
        WIA.ImageFile img = new WIA.ImageFile();
        img.LoadFile(filename);

        WIA.ImageProcess IP = new WIA.ImageProcess();

        Object ix1 = (Object)"RotateFlip";
        WIA.FilterInfo fi1 = IP.FilterInfos.get_Item(ref ix1);
        IP.Filters.Add(fi1.FilterID, 0);

        Object p1 = (Object)"RotationAngle";
        Object pv1 = (Object)angle;
        IP.Filters[1].Properties.get_Item(ref p1).set_Value(ref pv1);

        img = IP.Apply(img);

        File.Delete(filename);
        img.SaveFile(filename);


        BitmapImage imagetemp = new BitmapImage();
        using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            imagetemp.BeginInit();
            imagetemp.CacheOption = BitmapCacheOption.OnLoad;
            imagetemp.StreamSource = stream;
            imagetemp.EndInit();
        }

        return imagetemp;
    }

usage:

        string filename = System.AppDomain.CurrentDomain.BaseDirectory + "4.jpg";

        image.Source = rotateImage(filename,90);

Upvotes: 0

Jeb
Jeb

Reputation: 3789

Use the rotate flip method.

E.g.:

        Bitmap bitmap1 = (Bitmap)Bitmap.FromFile(@"C:\test.jpg");
        bitmap1.RotateFlip(RotateFlipType.Rotate180FlipNone);
        bitmap1.Save(@"C:\Users\Public\Documents\test rotated.jpg");

Upvotes: 5

Related Questions