如何在xamarin.forms中调整图像大小?

时间:2015-11-17 06:11:12

标签: xamarin.forms

我正在研究xamarin.forms。我必须从图库中选择图像,然后调整它们的大小,然后将它们上传到服务器上。但我不知道如何调整特定尺寸的选定图像大小? 请告诉我如何做到这一点?

4 个答案:

答案 0 :(得分:3)

这可以与流一起使用(如果您使用的是Media Plugin https://github.com/jamesmontemagno/MediaPlugin)或标准字节数组。

// If you already have the byte[]
byte[] resizedImage = await CrossImageResizer.Current.ResizeImageWithAspectRatioAsync(originalImageBytes, 500, 1000);

// If you have a stream, such as:
// var file = await CrossMedia.Current.PickPhotoAsync(options);
// var originalImageStream = file.GetStream();
byte[] resizedImage = await CrossImageResizer.Current.ResizeImageWithAspectRatioAsync(originalImageStream, 500, 1000);

答案 1 :(得分:1)

这是从上面的链接中获取的代码。

iOS

public class MediaService : IMediaService
{

    public byte[] ResizeImage(byte[] imageData, float width, float height)
    {

        UIImage originalImage = ImageFromByteArray(imageData);

        var originalHeight = originalImage.Size.Height;
        var originalWidth = originalImage.Size.Width;

        nfloat newHeight = 0;
        nfloat newWidth = 0;

        if (originalHeight > originalWidth)
        {
            newHeight = height;
            nfloat ratio = originalHeight / height;
            newWidth = originalWidth / ratio;
        }
        else 
        {
            newWidth = width;
            nfloat ratio = originalWidth / width;
            newHeight = originalHeight / ratio;
        }

        width = (float)newWidth;
        height = (float)newHeight;

        UIGraphics.BeginImageContext(new SizeF(width, height));
        originalImage.Draw(new RectangleF(0, 0, width, height));
        var resizedImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

        var bytesImagen = resizedImage.AsJPEG().ToArray();
        resizedImage.Dispose();
        return bytesImagen;
   }          
}

安卓

public class MediaService : IMediaService
{

    public byte[] ResizeImage(byte[] imageData, float width, float height)
    {
        // Load the bitmap 
        BitmapFactory.Options options = new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
        options.InPurgeable = true; // inPurgeable is used to free up memory while required
        Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length, options);

        float newHeight = 0;
        float newWidth = 0;

        var originalHeight = originalImage.Height;
        var originalWidth = originalImage.Width;

        if (originalHeight > originalWidth)
        {
            newHeight = height;
            float ratio = originalHeight / height;
            newWidth = originalWidth / ratio;
        }
        else 
        {
            newWidth = width;
            float ratio = originalWidth / width;
            newHeight = originalHeight / ratio;
        }

        Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, (int)newHeight, true);

        originalImage.Recycle();

        using (MemoryStream ms = new MemoryStream())
        {
            resizedImage.Compress(Bitmap.CompressFormat.Png, 100, ms);

            resizedImage.Recycle();

            return ms.ToArray();
        }
    }

WinPhone

public class MediaService : IMediaService
{
    private MediaImplementation mi = new MediaImplementation();

    public byte[] ResizeImage(byte[] imageData, float width, float height)
    {
        byte[] resizedData;

        using (MemoryStream streamIn = new MemoryStream(imageData))
        {
            WriteableBitmap bitmap = PictureDecoder.DecodeJpeg(streamIn, (int)width, (int)height);

            float Height = 0;
            float Width = 0;

            float originalHeight = bitmap.PixelHeight;
            float originalWidth = bitmap.PixelWidth;

            if (originalHeight > originalWidth)
            {
                Height = height;
                float ratio = originalHeight / height;
                Width = originalWidth / ratio;
            }
            else
            {
                Width = width;
                float ratio = originalWidth / width;
                Height = originalHeight / ratio;
            }

            using (MemoryStream streamOut = new MemoryStream())
            {
                bitmap.SaveJpeg(streamOut, (int)Width, (int)Height, 0, 100);
                resizedData = streamOut.ToArray();
            }
        }
        return resizedData;
    }
}

编辑:如果您已经在项目中使用 FFImageLoading,那么您可以将其用于您的平台。

https://github.com/luberda-molinet/FFImageLoading

答案 2 :(得分:0)

我尝试使用"CrossImageResizer.Current...",但在"Media Plugin"中找不到。 相反,我找到了一个选项调用"MaxWidthHeight",该选项调用仅在您还添加"PhotoSize = PhotoSize.MaxWidthHeight" option时才起作用。

示例

 var file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions() { PhotoSize = PhotoSize.MaxWidthHeight, MaxWidthHeight = 600 });
 var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions { PhotoSize = PhotoSize.MaxWidthHeight, MaxWidthHeight = 600 });

答案 3 :(得分:0)

可悲的是,没有一个很好的跨平台图像缩放器(我在本文发布时已经找到)。图像处理并非真正针对在iOS和Android的跨平台环境中进行而设计的。使用特定于平台的代码在每个平台上执行此操作更快,更干净。您可以使用依赖项注入和DependencyService(或任何其他服务或IOC)来做到这一点。

AdamP对如何执行此操作Platform Specific Image Resizing

做出了很好的回应