C# 在c中旋转位图图像#

C# 在c中旋转位图图像#,c#,xaml,windows-phone-8.1,bitmapimage,storagefile,C#,Xaml,Windows Phone 8.1,Bitmapimage,Storagefile,我在使用BitmapImage时遇到一些问题。我有一个WP8.1应用程序,我可以在其中拍照,预览和上传。预览显示得很好,旋转和大小都很好,但问题是,为了上传它,我传递了与storageFile相对应的字节[],这是一张大的、方向性不太好的图片。。我想先旋转和收缩storageFile,然后再从中获取字节[] 下面是我用来预览/拍摄/转换图片的片段:) //Récupère la liste des camèras disponsibles 专用静态异步任务GetCameraID(Windows.

我在使用BitmapImage时遇到一些问题。我有一个WP8.1应用程序,我可以在其中拍照,预览和上传。预览显示得很好,旋转和大小都很好,但问题是,为了上传它,我传递了与storageFile相对应的字节[],这是一张大的、方向性不太好的图片。。我想先旋转和收缩storageFile,然后再从中获取字节[]

下面是我用来预览/拍摄/转换图片的片段:)

//Récupère la liste des camèras disponsibles
专用静态异步任务GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
DeviceInformation deviceID=(等待DeviceInformation.FindalAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x=>x.EnclosureLocation!=null&&x.EnclosureLocation.Panel==desiredCamera);
如果(deviceID!=null)返回deviceID;
else抛出新异常(string.Format(“类型为{0}的摄影机不存在。”,desiredCamera));
}
//初始化la caméra
异步私有void initCamera()
{
//可利用的咖啡杯
var cameraID=wait GetCameraID(Windows.Devices.Enumeration.Panel.Back);
//初始化l'objet de capture
captureManager=新媒体捕获();
等待captureManager.InitializeAsync(新的MediaCaptureInitializationSettings
{
StreamingCaptureMode=StreamingCaptureMode.Video,
PhotoCaptureSource=PhotoCaptureSource.VideoPreview,
AudioDeviceId=string.Empty,
VideoDeviceId=cameraID.Id
});
var maxResolution=captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).聚合((i1,i2)=>(i1作为视频编码属性).宽度>(i2作为视频编码属性).宽度?i1:i2);
等待captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo,maxResolution);
}
//埃文梅内特酒店。
异步私有void预览图片_单击(对象发送方,路由目标)
{
//这是一种视觉化的语言
如果(EnPreview==false)
{
//在缓存中,用户可以使用。
tfComment.Visibility=Visibility.Collapsed;
vbImgDisplay.Visibility=可见性。已折叠;
//在伦敦市中心的观想广场上。
captureManager.设置预览旋转(视频旋转,顺时针90度);
//关于对《芬特的形象》进行视觉化的指控。
capturePreview.Source=captureManager;
//在rend la pré上,可视化是可能的。
capturePreview.Visibility=可见性.Visibility;
//视觉化通量酒店
等待captureManager.StartPreviewSync();
//关于视觉化的实践是一门艺术。
EnPreview=true;
//关于更改布顿标签
previewpicture.Label=“Prendre photo”;
}
//视觉化是一门艺术
else if(EnPreview==true)
{
//在应用程序的内部莫尔条纹照片设计
StorageFile photoFile=wait ApplicationData.Current.LocalFolder.CreateFileAsync(“myFirstPhoto.jpg”,CreationCollisionOption.ReplaceExisting);
//Récupère la photo avec le format d'encodage décidé。
等待captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(),photoFile);
//我们还可以拍摄一张照片到内存流
InMemoryRandomAccessStream memStream=新建InMemoryRandomAccessStream();
等待captureManager.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpegXR(),memStream);
//Debug.WriteLine(memStream.ToString());
//这是一张关于集装箱的图片
BitmapImage bitmapToShow=新的BitmapImage(新的Uri(photoFile.Path));
//Met l'image dans le container d'affichage
imgDisplay.Source=bitmapToShow;
vbImgDisplay.RenderTransform=新的旋转变换(){CenterX=0.5,CenterY=0.5,Angle=90};
//预视觉
capturePreview.Visibility=Visibility.Collapsed;
//视觉化通量
等待captureManager.stopReviewSync();
//撕裂可见的组件
tfComment.Visibility=可见性.Visibility;
vbImgDisplay.Visibility=可见性.Visibility;
//字节表中的图像转换
photoByte=等待GetByteFromFile(photoFile);
//对est+视觉化的精确理解
EnPreview=false;
previewpicture.Label=“Prévisualizer”;
}
}
//转换存储文件的字节[]
专用异步任务GetByteFromFile(StorageFile-StorageFile)
{
var stream=await-storageFile.OpenReadAsync();
使用(var dataReader=newdatareader(流))
{
var bytes=新字节[stream.Size];
等待dataReader.LoadAsync((uint)stream.Size);
dataReader.ReadBytes(字节);
返回字节;
}
}

提前谢谢你的帮助

我建议您使用Lumia(诺基亚)图像SDK来旋转图像,这样更容易

你可以在这里找到它们:

您可以像这样旋转图像:

using (var filterEffect = new FilterEffect(source))
{
    // Initialize the filter and add the filter to the FilterEffect collection to rotate for 15 degrees
    var filter = new RotationFilter(15.0);

    filterEffect.Filters = new IFilter[] { filter };

    // Create a target where the filtered image will be rendered to
    var target = new WriteableBitmap(width, height);

    // Create a new renderer which outputs WriteableBitmaps
    using (var renderer = new WriteableBitmapRenderer(filterEffect, target))
    {
        // Render the image with the filter(s)
        await renderer.RenderAsync();

        // Set the output image to Image control as a source
        ImageControl.Source = target;
    }

    await SaveEffectAsync(filterEffect, "RotationFilter.jpg", outputImageSize);
}
using (var filterEffect = new FilterEffect(source))
{
    // Initialize the filter and add the filter to the FilterEffect collection to rotate for 15 degrees
    var filter = new RotationFilter(15.0);

    filterEffect.Filters = new IFilter[] { filter };

    // Create a target where the filtered image will be rendered to
    var target = new WriteableBitmap(width, height);

    // Create a new renderer which outputs WriteableBitmaps
    using (var renderer = new WriteableBitmapRenderer(filterEffect, target))
    {
        // Render the image with the filter(s)
        await renderer.RenderAsync();

        // Set the output image to Image control as a source
        ImageControl.Source = target;
    }

    await SaveEffectAsync(filterEffect, "RotationFilter.jpg", outputImageSize);
}
 //this method will rotate an image any degree
   public static Bitmap RotateImage(Bitmap image, float angle)
    {
        //create a new empty bitmap to hold rotated image

        double radius = Math.Sqrt(Math.Pow(image.Width, 2) + Math.Pow(image.Height, 2));

        Bitmap returnBitmap = new Bitmap((int)radius, (int)radius);

        //make a graphics object from the empty bitmap
        using (Graphics graphic = Graphics.FromImage(returnBitmap))
        {
            //move rotation point to center of image
            graphic.TranslateTransform((float)radius / 2, (float)radius / 2);
            //rotate
            graphic.RotateTransform(angle);
            //move image back
            graphic.TranslateTransform(-(float)image.Width / 2, -(float)image.Height / 2);
            //draw passed in image onto graphics object
            graphic.DrawImage(image, new Point(0, 0));
        }
        return returnBitmap;
    }