简体   繁体   English

C#StorageFile图像大小调整为一定数量的字节

[英]C# StorageFile image resize to certain amount of bytes

I have already read a lot of posts on Stack about image resizing and quality reducing, but non of them has been about reducing quality to certain physical disk space 我已经在Stack上阅读了很多有关调整图像大小和降低质量的文章,但是没有一篇关于降低某些物理磁盘空间的质量的文章。

I have a code that takes a picture: 我有一个拍照的代码:

private async void TakeAPhoto_Click(object sender, RoutedEventArgs e)
{
    CameraCaptureUI captureUI = new CameraCaptureUI();
    captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;

    StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (photo != null)
    {

    }
}

Now i need to send the data to the server, but before, i need to ensure that the photo is not more than 3 MB. 现在,我需要将数据发送到服务器,但是在此之前,我需要确保照片不超过3 MB。

So i do this: 所以我这样做:

BasicProperties pro = await photo.GetBasicPropertiesAsync();
if (pro.Size < 3072)
{
    // SEND THE FILE TO SERVER
}
else
{
    // DECREASE QUALITY BEFORE SENDING
}

So the question is about ELSE block 所以问题是关于ELSE区块

Is there a better or maybe i have missed some built-in approach to fit image into the certain amount of megabytes by decreasing it's quality? 有没有更好的选择,或者我错过了一些内置的方法来通过降低图像质量来将图像调整为一定的兆字节?

Because doing this: 因为这样做:

while (pro.Size <= 3072)
{
    photo = // some logic to decrease quality on 10%
}

Doesn't really look good. 看起来真的不太好。

Just Create one function : 只需创建一个功能:

    /// <summary>
    /// function to reduce image size and returns local path of image
    /// </summary>
    /// <param name="scaleFactor"></param>
    /// <param name="sourcePath"></param>
    /// <param name="targetPath"></param>
    /// <returns></returns>
    private string ReduceImageSize(double scaleFactor, Stream sourcePath, string targetPath)
    {
        try
        {
            using (var image = System.Drawing.Image.FromStream(sourcePath))
            {
                //var newWidth = (int)(image.Width * scaleFactor);
                //var newHeight = (int)(image.Height * scaleFactor);


                var newWidth = (int)1280;
                var newHeight = (int)960;

                var thumbnailImg = new System.Drawing.Bitmap(newWidth, newHeight);
                var thumbGraph = System.Drawing.Graphics.FromImage(thumbnailImg);
                thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
                var imageRectangle = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);
                thumbGraph.DrawImage(image, imageRectangle);
                thumbnailImg.Save(targetPath, image.RawFormat);
                return targetPath;



            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception in ReduceImageSize" + e);
            return "";
        }
    }

And then call this function in your else block as below you will get same image with reduced size : 然后在您的else块中调用此函数,如下所示,您将获得相同大小的图像:

        string ImageLink = "https://imagesus-ssl.homeaway.com/mda01/337b3cbe-80cf-400a-aece-c932852eb929.1.10";
        string FinalTargetPath=@"F:\ReducedImage.png";
        HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(ImageLink);
        WebResponse imageResponse = imageRequest.GetResponse();
        Stream responseStream = imageResponse.GetResponseStream();

       string ImagePath= ReduceImageSize(0.5, responseStream, FinalTargetPath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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