gpt4 book ai didi

c# - 从库中选择图像并压缩 windows phone 8.1

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

在这段代码中,我首先从 Windows Phone 照片库中选取图像,然后将其压缩以便上传。但是我在压缩时遇到了一些问题,因为当我在 BitmapEncoder 中使用 FlushAsync 时,原始的 final 没有被覆盖并且发生异常。这是我的代码

从库中选择图片

ImagePath = string.Empty;
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;

// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.PickMultipleFilesAndContinue();
view.Activated += viewActivated;

解码和压缩图像

FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
if (args != null)
{
try
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
userLoginResponse = await DataManager.GetLoginResponseAsync();
for (int no = 0; (no < args.Files.Count && no < 4); no++)
{
StorageFile storageFile = args.Files[no];
var stream = await storageFile.OpenAsync(FileAccessMode.ReadWrite);

var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var decoder = await BitmapDecoder.CreateAsync(stream);
PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
var data = pixelData.DetachPixelData();

BitmapPropertySet propertySet = new BitmapPropertySet();
BitmapTypedValue qualityValue = new BitmapTypedValue(1.0, Windows.Foundation.PropertyType.Single);
propertySet.Add("ImageQuality", qualityValue);
BitmapEncoder be = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
be.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, 480, 480, 50, 50, data);
await be.FlushAsync();

任何人都可以提出解决此问题的方法。提前致谢

最佳答案

我使用诺基亚成像 SDK 来完成此任务。只需将 SDK 添加到您的解决方案中 using NuGet.然后包括

using Lumia.Imaging;

在您的代码文件中。之后,您可以使用以下代码从 StorageFile 读取图像数据并将其压缩为 byte[],然后您可以将其上传到您的服务器:

byte[] compressedImageBytes = null;
IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read); // "file" being the StorageFile from your FileOpenPicker
if (file.Name.ToLower().EndsWith(".jpg") || file.Name.ToLower().EndsWith(".jpeg"))
{
using (StreamImageSource source = new StreamImageSource(fileStream.AsStreamForRead()))
{
IFilterEffect effect = new FilterEffect(source);
using (JpegRenderer renderer = new JpegRenderer(effect))
{
renderer.Quality = 0.7; // higher value means better quality
IBuffer buffer = await renderer.RenderAsync();
Windows.Security.Cryptography.CryptographicBuffer.CopyToByteArray(buffer, out compressedImageBytes);
}
}
}}

if (compressedImageBytes != null)
{
//you can upload or safe the byte-array of the compressed image
}

关于c# - 从库中选择图像并压缩 windows phone 8.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29530998/

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