我已经在Stack上阅读了很多关于图像大小调整和质量降低的帖子,但其中没有关于降低某些物理磁盘空间质量的帖子
我有一个拍照的代码:
import android.view.viewgroup.layoutparams;
现在我需要将数据发送到服务器,但在此之前,我需要确保照片不超过3 MB。
所以我这样做:
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)
{
}
}
所以问题是关于ELSE阻止
有没有更好的,或者我错过了一些内置的方法,通过降低图像的质量来将图像调整到一定的兆字节数?
因为这样做:
BasicProperties pro = await photo.GetBasicPropertiesAsync();
if (pro.Size < 3072)
{
// SEND THE FILE TO SERVER
}
else
{
// DECREASE QUALITY BEFORE SENDING
}
看起来不太好。
答案 0 :(得分:0)
只需创建一个功能:
/// <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);