gpt4 book ai didi

c# phone 8.1 更改 jpeg 质量

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:47 24 4
gpt4 key购买 nike

我想从相机捕捉图像,减小其尺寸并降低质量。

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

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();

}

最佳答案

罗伯特提到的链接为我打开了文章的 HTML 版本,但是 XAML one有一些很好的样本:

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.
}

关于c# phone 8.1 更改 jpeg 质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27027643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com