8,735
社区成员
发帖
与我相关
我的任务
分享
public static async Task<ImageSource> GetWebImageAsync(string imageURI)
{
//var imageUrl = "http://www.microsoft.com/global/en-us/news/publishingimages/logos/MSFT_logo_Web.jpg";
var client = new HttpClient();
Stream stream = await client.GetStreamAsync(imageURI);
return await GetImageAsync(stream);
}
public static async Task<byte[]> GetImageBytesAsync(PickerLocationId location, uint maxSizeMB)
{
try
{
FileOpenPicker fop = new FileOpenPicker();
fop.FileTypeFilter.Add(".jpg");
fop.FileTypeFilter.Add(".jpeg");
fop.FileTypeFilter.Add(".png");
fop.SuggestedStartLocation = location;
fop.ViewMode = PickerViewMode.Thumbnail;
var chooseImageFile = await fop.PickSingleFileAsync();
if (chooseImageFile == null)
return null;
using (var accessStream = await chooseImageFile.OpenReadAsync())
{
var filePro = await chooseImageFile.GetBasicPropertiesAsync();
if (filePro.Size / 1024 / 1024 > maxSizeMB)
{
await BanYiUWPMessageHelper.ShowMessageDialogAsync("照片大小超过设置值,请重新选择。 ");
return null;
}
var bmpStream = accessStream.AsStream();
var imageBytes = await GetBytesAsync(bmpStream);
return imageBytes;
//BitmapImage bmp = new BitmapImage();
//await bmp.SetSourceAsync(accessStream);
//return bmp;
}
}
catch
{
return null;
}
}
public static async Task<Tuple<byte[], StorageFile>> GetImageAsync(PickerLocationId location, uint maxSizeMB)
{
try
{
FileOpenPicker fop = new FileOpenPicker();
fop.FileTypeFilter.Add(".jpg");
fop.FileTypeFilter.Add(".jpeg");
fop.FileTypeFilter.Add(".png");
fop.SuggestedStartLocation = location;
fop.ViewMode = PickerViewMode.Thumbnail;
var chooseImageFile = await fop.PickSingleFileAsync();
if (chooseImageFile == null)
return null;
using (var accessStream = await chooseImageFile.OpenReadAsync())
{
var filePro = await chooseImageFile.GetBasicPropertiesAsync();
if (filePro.Size / 1024 / 1024 > maxSizeMB)
{
await BanYiUWPMessageHelper.ShowMessageDialogAsync("照片大小超过设置值,请重新选择。 ");
return null;
}
var bmpStream = accessStream.AsStream();
var imageBytes = await GetBytesAsync(bmpStream);
Tuple<byte[], StorageFile> result = new Tuple<byte[], StorageFile>(imageBytes, chooseImageFile);
return result;
//BitmapImage bmp = new BitmapImage();
//await bmp.SetSourceAsync(accessStream);
//return bmp;
}
}
catch
{
return null;
}
}
public static async Task<StorageFile> GetImageFileAsync(PickerLocationId location, uint maxSizeMB)
{
try
{
FileOpenPicker fop = new FileOpenPicker();
fop.FileTypeFilter.Add(".jpg");
fop.FileTypeFilter.Add(".jpeg");
fop.FileTypeFilter.Add(".png");
fop.SuggestedStartLocation = location;
fop.ViewMode = PickerViewMode.Thumbnail;
var chooseImageFile = await fop.PickSingleFileAsync();
if (chooseImageFile == null)
return null;
using (var accessStream = await chooseImageFile.OpenReadAsync())
{
var filePro = await chooseImageFile.GetBasicPropertiesAsync();
if (filePro.Size / 1024 / 1024 > maxSizeMB)
{
await BanYiUWPMessageHelper.ShowMessageDialogAsync("照片大小超过设置值,请重新选择。 ");
return null;
}
return chooseImageFile;
//BitmapImage bmp = new BitmapImage();
//await bmp.SetSourceAsync(accessStream);
//return bmp;
}
}
catch
{
return null;
}
}
public static async Task<bool> SaveImageAsync(ImageSource imgSource, string saveFileName, PickerLocationId location)
{
var imgBytes = GetBytes(imgSource);
return await SaveImageAsync(saveFileName, location, imgBytes);
}
private static async Task<bool> SaveImageAsync(string saveFileName, PickerLocationId location, byte[] imgBytes)
{
StorageFile saveFile = await OpenSaveFilePicker(saveFileName, location);
if (saveFile == null)
return false;
CachedFileManager.DeferUpdates(saveFile);
await FileIO.WriteBytesAsync(saveFile, imgBytes);
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(saveFile);
if (status == FileUpdateStatus.Complete)
{
return true;
}
else
{
return false;
}
}
public static async Task<bool> SaveImageAsync(UIElement ue, string saveFileName, PickerLocationId location)
{
var imgBytes = await GetBytesAsync(ue);
return await SaveImageAsync(saveFileName, location, imgBytes);
}
private static async Task<StorageFile> OpenSaveFilePicker(string saveFileName, PickerLocationId location)
{
FileSavePicker fsp = new FileSavePicker();
fsp.DefaultFileExtension = ".png";
fsp.FileTypeChoices.Add("png", new List<string>() { ".jpg", ".jpeg", ".png" });
fsp.SuggestedFileName = saveFileName;
fsp.SuggestedStartLocation = location;
var saveFile = await fsp.PickSaveFileAsync();
return saveFile;
}
public static async Task<bool> SaveImageAsPng(UIElement ue, string saveFileName)
{
try
{
StorageFolder savedPics = KnownFolders.PicturesLibrary;
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(ue);
IBuffer buffer = await rtb.GetPixelsAsync();
StorageFile newFile = await savedPics.CreateFileAsync(saveFileName, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream streamOut = await newFile.OpenAsync(FileAccessMode.ReadWrite);
BitmapEncoder pngEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, streamOut);
byte[] data = buffer.ToArray();
pngEncoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
(uint)rtb.PixelWidth,
(uint)rtb.PixelHeight,
96d, 96d, data);
await pngEncoder.FlushAsync();
streamOut.Dispose();
return true;
}
catch
{
return false;
}
}
public static async Task<IRandomAccessStream> GetRandomAccessStream(byte[] input)
{
//using (MemoryStream stream = new MemoryStream(input))
//{
var randomAccessStream = new InMemoryRandomAccessStream();
var outputStream = randomAccessStream.GetOutputStreamAt(0);
var dw = new DataWriter(outputStream);
var task = new Task(() => dw.WriteBytes(input));
task.Start();
await task;
await dw.StoreAsync();
var success = await outputStream.FlushAsync();
randomAccessStream.Seek(0);
return randomAccessStream;
//}
}
public static async Task<ImageSource> GetImageAsync(Stream imageStream)
{
if (imageStream == null || imageStream.Length == 0)
return null;
BitmapImage bmpImage = new BitmapImage();
var imageBytes = new Byte[imageStream.Length];
int restLength = 0;
//while (true)
//{
restLength = await imageStream.ReadAsync(imageBytes, 0, (int)imageStream.Length);
//if (restLength == 0)
// break;
//}
return await GetImageAsync(imageBytes);
}
public static async Task<byte[]> GetBytesAsync(Stream imageStream)
{
if (imageStream == null || imageStream.Length == 0)
return null;
var imageBytes = new Byte[imageStream.Length];
imageStream.Position = 0;
imageStream.Seek(0, SeekOrigin.Begin);
await imageStream.ReadAsync(imageBytes, 0, (int)imageStream.Length);
return imageBytes;
#region 分步读取stream
//int readLength = 0;
//int writeOffset = 0;
//int readCount = 1024;
//while (true)
//{
// if (writeOffset + readCount <= imageStream.Length)
// readCount = 1024;
// else
// readCount = (int)(imageStream.Length - writeOffset);
// readCount = readCount >= 0 ? readCount : 0;
// readLength = await imageStream.ReadAsync(imageBytes, writeOffset, readCount);
// if (readLength == 0)
// break;
// writeOffset = writeOffset + readLength;
//}
#endregion
}
public static async Task<byte[]> GetBytesAsync1(Stream input)
{
if (input == null || input.Length == 0)
return null;
byte[] buffer = new byte[1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
await ms.FlushAsync();
ms.Seek(0, SeekOrigin.Begin);
return ms.ToArray();
}
}
public static async Task<ImageSource> GetImageAsync(UIElement ue)
{
if (ue == null)
return null;
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(ue);
return rtb;
}
public static async Task<byte[]> GetBytesAsync(UIElement ue)
{
if (ue == null)
return null;
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(ue);
var buffer = await rtb.GetPixelsAsync();
return buffer.ToArray();
}
public static Stream GetStream(byte[] inputBytes)
{
MemoryStream ms = new MemoryStream(inputBytes);
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
public static async Task<BitmapImage> GetImageAsync1(byte[] pixeByte)
{
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
stream.Seek(0);
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(pixeByte, 0, pixeByte.Length);
await stream.WriteAsync(buffer);
BitmapImage image = new BitmapImage();
await image.SetSourceAsync(stream);
return image;
}
}
public static async Task<ImageSource> GetImageAsync(byte[] imageBytes)
{
if (imageBytes == null || imageBytes.Length == 0)
return null;
try
{
MemoryStream ms = new MemoryStream(imageBytes);
var ras = ms.AsRandomAccessStream();
BitmapImage bmp = new BitmapImage();
await bmp.SetSourceAsync(ras);
return bmp;
}
catch
{
return null;
}
}
public static byte[] GetBytes(Image img)
{
if (img == null)
return null;
if (img.Source == null)
return null;
try
{
WriteableBitmap wb = img.Source as WriteableBitmap;
return wb.PixelBuffer.ToArray();
}
catch
{
return null;
}
}
public static byte[] GetBytes(ImageSource imgSource)
{
if (imgSource == null)
return null;
try
{
WriteableBitmap wb = imgSource as WriteableBitmap;
return wb.PixelBuffer.ToArray();
}
catch
{
return null;
}
}
public static byte[] GetBytes(Stream stm)
{
if (stm == null || stm.Length == 0)
return null;
using (BinaryReader br = new BinaryReader(stm))
{
return br.ReadBytes((int)stm.Length);
}
}
public static async Task<MemoryStream> GetStreamAsync(Stream stream)
{
if (stream == null || stream.Length == 0)
return null;
MemoryStream memoryStream = new MemoryStream();
byte[] buffer = await GetBytesAsync1(stream);
if (buffer == null)
return null;
var binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(buffer);
return memoryStream;
}