以 bytes[] 为单位将图像调整为不同大小的图像,也以 bytes[] - Windows 8
Resize image in bytes[] to a different sized image also in bytes[] - Windows 8
我有这段代码可以从网络服务中检索图像并将其保存到 StorageFile
StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(Constants.DataDirectory, CreationCollisionOption.OpenIfExists);
StorageFile imgFile;
using (var httpClient = new HttpClient { BaseAddress = Constants.baseAddress })
{
string token = App.Current.Resources["token"] as string;
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
using (var response2 = await httpClient.GetAsync("user/image?userId=" + id))
{
Stream imageStream = await response2.Content.ReadAsStreamAsync();
if (imageStream.Length != 0)
{
byte[] bytes = new byte[imageStream.Length];
imageStream.Read(bytes, 0, (int)imageStream.Length);
imgFile = await folder.CreateFileAsync(fname, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(imgFile, bytes); //i want the image bytes to be of a smaller version of the image at this point
}
return await response2.Content.ReadAsStringAsync();
}
}
我想知道是否有办法在将图像 bytes[]
写入 StorageFile
之前将其转换为缩略图版本(例如 50x50)。
这是调整图像流大小的代码:
var decoder = await BitmapDecoder.CreateAsync(stream);
var pixels = await decoder.GetPixelDataAsync();
var file = await folder.CreateFileAsync($"{_oriFile.DisplayName}_{size}.png",CreationCollisionOption.GenerateUniqueName);
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);
encoder.BitmapTransform.ScaledWidth = size;
encoder.BitmapTransform.ScaledHeight = size;
encoder.SetPixelData(
decoder.BitmapPixelFormat,
decoder.BitmapAlphaMode,
decoder.PixelWidth, decoder.PixelHeight,
decoder.DpiX, decoder.DpiY,
pixels.DetachPixelData());
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
await encoder.FlushAsync();
}
当您获得图像流时,您应该在将其保存为 StorageFile 之前调整大小。当然你可以在保存到StorageFile之后再做。
我有这段代码可以从网络服务中检索图像并将其保存到 StorageFile
StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(Constants.DataDirectory, CreationCollisionOption.OpenIfExists);
StorageFile imgFile;
using (var httpClient = new HttpClient { BaseAddress = Constants.baseAddress })
{
string token = App.Current.Resources["token"] as string;
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
using (var response2 = await httpClient.GetAsync("user/image?userId=" + id))
{
Stream imageStream = await response2.Content.ReadAsStreamAsync();
if (imageStream.Length != 0)
{
byte[] bytes = new byte[imageStream.Length];
imageStream.Read(bytes, 0, (int)imageStream.Length);
imgFile = await folder.CreateFileAsync(fname, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(imgFile, bytes); //i want the image bytes to be of a smaller version of the image at this point
}
return await response2.Content.ReadAsStringAsync();
}
}
我想知道是否有办法在将图像 bytes[]
写入 StorageFile
之前将其转换为缩略图版本(例如 50x50)。
这是调整图像流大小的代码:
var decoder = await BitmapDecoder.CreateAsync(stream);
var pixels = await decoder.GetPixelDataAsync();
var file = await folder.CreateFileAsync($"{_oriFile.DisplayName}_{size}.png",CreationCollisionOption.GenerateUniqueName);
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);
encoder.BitmapTransform.ScaledWidth = size;
encoder.BitmapTransform.ScaledHeight = size;
encoder.SetPixelData(
decoder.BitmapPixelFormat,
decoder.BitmapAlphaMode,
decoder.PixelWidth, decoder.PixelHeight,
decoder.DpiX, decoder.DpiY,
pixels.DetachPixelData());
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
await encoder.FlushAsync();
}
当您获得图像流时,您应该在将其保存为 StorageFile 之前调整大小。当然你可以在保存到StorageFile之后再做。