C# 手机 8.1 更改 JPEG 质量

本文关键字:JPEG 质量 更改 手机 | 更新日期: 2023-09-27 18:31:17

我想从相机捕获图像,减小其大小并降低质量。

这是我到目前为止的功能:

private async void BildSpeichern(object sender, RoutedEventArgs e)
{
    MenuFlyoutItem FO = sender as MenuFlyoutItem;
    StorageFile datei = await StorageFile.GetFileFromPathAsync(_viewmodel.DateiPfad);
    string neuerDateiName=datei.Name + "___" + FO.CommandParameter.ToString();
    await datei.RenameAsync(neuerDateiName);
    _viewmodel.DateiPfad = datei.Path;
    using (var sourceStream = await datei.OpenAsync(FileAccessMode.ReadWrite))
    {
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapEncoder.JpegEncoderId, sourceStream);

        uint oriWidth = decoder.PixelWidth;
        uint oriHeight = decoder.PixelHeight;
        uint tarWidth;
        uint tarHeight;
        if (oriWidth > oriHeight)
        {
            tarWidth = 1024;
            tarHeight = (uint)((float)tarWidth / (float)oriWidth * (float)oriHeight);
        }
        else
        {
            tarHeight = 1024;
            tarWidth = (uint)((float)tarHeight / (float)oriHeight * (float)oriWidth);
        }
        InMemoryRandomAccessStream IS = new InMemoryRandomAccessStream();
        BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(IS, decoder);
        decoder = null;
        encoder.BitmapTransform.ScaledWidth = tarHeight;
        encoder.BitmapTransform.ScaledHeight = tarWidth;
        await encoder.FlushAsync();
    }
    await datei.CopyAsync(Windows.Storage.KnownFolders.PicturesLibrary,"TEST.JPG",NameCollisionOption.ReplaceExisting);
    FotoVorschau.Visibility = Visibility.Visible;
    FotoAnzeige.Visibility = Visibility.Collapsed;
    AufnahmeStackpanel.Visibility = Visibility.Visible;
    VorschauStackpanel.Visibility = Visibility.Collapsed;
    FotoAnzeige.Source = null;
    _viewmodel.DateiPfad = null;
}

我唯一缺少的是压缩,或者更好地说是生成的JPEG图像的质量降低。

我找到的唯一示例使用System.Drawing,但这在Windows Phone 8.1项目中不可用。有人知道如何实现这一目标吗?

编辑:这是我函数的新部分,但图像不会调整大小,也不会被压缩:

using (var sourceStream = await datei.OpenAsync(FileAccessMode.ReadWrite))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);

                uint oriWidth = decoder.PixelWidth;
                uint oriHeight = decoder.PixelHeight;
                uint tarWidth;
                uint tarHeight;
                if (oriWidth > oriHeight)
                {
                    tarWidth = 1024;
                    tarHeight = (uint)((float)tarWidth / (float)oriWidth * (float)oriHeight);
                }
                else
                {
                    tarHeight = 1024;
                    tarWidth = (uint)((float)tarHeight / (float)oriHeight * (float)oriWidth);
                }
                var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet();
                var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue(
                    0.6,
                    Windows.Foundation.PropertyType.Single
                    );
                propertySet.Add("ImageQuality", qualityValue);
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, sourceStream, propertySet);
                PixelDataProvider pd = await decoder.GetPixelDataAsync();
                encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, oriWidth, oriHeight, decoder.DpiX, decoder.DpiY, pd.DetachPixelData());
                decoder = null;
                encoder.BitmapTransform.ScaledWidth = tarHeight;
                encoder.BitmapTransform.ScaledHeight = tarWidth;
                encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Cubic;
                await encoder.FlushAsync();
                sourceStream.Dispose();
            }

C# 手机 8.1 更改 JPEG 质量

Robert 提到的链接为我打开了文章的 HTML 版本,但 XAML 有一些很好的示例:

IAsyncOperation<BitmapEncoder> CreateEncoderWithEncodingOptionsAsync(
    Windows.Storage.Streams.IRandomAccessStream stream
    )
{
    var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet();
    var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue(
        1.0, // Maximum quality
        Windows.Foundation.PropertyType.Single
        );
    propertySet.Add("ImageQuality", qualityValue);
    return Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(
        Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId,
        stream,
        propertySet
        );
    // Encoder is initialized with encoding options.
}
值得

一看的是以下内容:

http://msdn.microsoft.com/library/windows/apps/system.windows.media.imaging.extensions.savejpeg(v=vs.105).aspx

System.Windows.Media.Imaging 命名空间中的 Extensions.SaveJpeg 方法。特别是这种方法的质量参数:

质量类型:系统.Int32此参数表示 JPEG 照片的质量,范围介于 0 和 100 之间,其中 100 表示最佳照片质量。我们建议您不要低于值 70。因为 JPEG 图像质量在该级别以下会显着降低。

然后可以使用如下代码调用它:

using (IsolatedStorageFileStream isostream = iso.CreateFile(strImageName)) 
{                    
    BitmapImage bitmap = new BitmapImage(); 
    bitmap.SetSource(stream); 
    WriteableBitmap wb = new WriteableBitmap(bitmap); 
    // Encode WriteableBitmap object to a JPEG stream. 
    Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
    isostream.Close(); 
} 

下面是有关编码选项的一些额外信息,以及有关 ImageQuality 编码选项的更多详细信息。

http://msdn.microsoft.com/en-us/library/windows/apps/jj218354.aspx

Filip 的示例有效,但最后缺少一行代码:

propertySet.Add("ImageQuality", qualityValue);

您可以在本文中找到示例

IAsyncOperation<BitmapEncoder> CreateEncoderWithEncodingOptionsAsync(
Windows.Storage.Streams.IRandomAccessStream stream)
{
var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet();
var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue(
    1.0, // Maximum quality
    Windows.Foundation.PropertyType.Single
    );
propertySet.Add("ImageQuality", qualityValue);
return Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(
    Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId,
    stream,
    propertySet
    );
propertySet.Add("ImageQuality", qualityValue);
}