【问题标题】:C# StorageFile image resize to certain amount of bytesC# StorageFile 图像大小调整为一定的字节数
【发布时间】:2016-12-27 11:39:29
【问题描述】:

我已经在 Stack 上阅读了很多关于调整图像大小和降低质量的帖子,但没有一篇是关于降低某些物理磁盘空间的质量

我有一个可以拍照的代码:

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

    }
}

现在我需要将数据发送到服务器,但之前,我需要确保照片不超过 3 MB。

所以我这样做:

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

所以问题是关于 ELSE 块

有没有更好的方法,或者我错过了一些通过降低图像质量来将图像放入一定数量的兆字节的内置方法?

因为这样做:

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

确实不好看。

【问题讨论】:

  • 我认为没有更好的方法。是的,您可以应用一些启发式方法(例如,如果文件比您需要的大得多 - 将质量降低 10% 以上),但仍然会有一个循环和多次质量降低(如果降低质量无济于事,甚至会降低尺寸) .

标签: c# uwp bitmapimage storagefile


【解决方案1】:

只需创建一个函数:

    /// <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 "";
        }
    }

然后在你的 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);

【讨论】:

  • 感谢缩小尺寸的代码,但如果我可以摆脱这个,主要部分很有趣:while (pro.Size
猜你喜欢
  • 1970-01-01
  • 2021-09-06
  • 2022-01-22
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 2011-10-24
  • 2020-06-27
  • 1970-01-01
相关资源
最近更新 更多