简体   繁体   中英

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?

Use the rotate flip method.

Eg:

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM