本文整理汇总了C#中IRandomAccessStream类的典型用法代码示例。如果您正苦于以下问题:C# IRandomAccessStream类的具体用法?C# IRandomAccessStream怎么用?C# IRandomAccessStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IRandomAccessStream类属于命名空间,在下文中一共展示了IRandomAccessStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadSurfaceAsync
public async Task LoadSurfaceAsync(ICanvasResourceCreator creator, IRandomAccessStream stream)
{
tempSurface = await CanvasBitmap.LoadAsync(creator, stream);
bound = tempSurface.Bounds;
center = tempSurface.Size.ToVector2() / 2;
blur.Source = tempSurface;
}
示例2: InternalSaveAsync
/// <summary>
/// Saves the file with fullFilePath, uses FileMode.Create, so file create time will be rewrited if needed
/// If exception has occurred while writing the file, it will delete it
/// </summary>
/// <param name="fullFilePath">example: "\\image_cache\\213898adj0jd0asd</param>
/// <param name="cacheStream">stream to write to the file</param>
/// <returns>true if file was successfully written, false otherwise</returns>
protected async virtual Task<bool> InternalSaveAsync(string fullFilePath, IRandomAccessStream cacheStream)
{
var storageFile = await SF.CreateFileAsync(fullFilePath, CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream outputStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
{
try
{
await RandomAccessStream.CopyAsync(
cacheStream.GetInputStreamAt(0L),
outputStream.GetOutputStreamAt(0L));
return true;
}
catch
{
try
{
// If file was not saved normally, we should delete it
await storageFile.DeleteAsync();
}
catch
{
ImageLog.Log("[error] can not delete unsaved file: " + fullFilePath);
}
}
}
ImageLog.Log("[error] can not save cache to the: " + fullFilePath);
return false;
}
示例3: ResizeImageHard
/// <summary>
/// 改变图片的大小
/// </summary>
/// <param name="sourceStream">包含图片数据的流</param>
/// <param name="expWidth">期望的宽度</param>
/// <param name="expHeight">期望的高度</param>
/// <returns></returns>
public static async Task<IRandomAccessStream> ResizeImageHard(IRandomAccessStream sourceStream, uint expWidth,uint expHeight)
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);
uint height = decoder.PixelHeight;
uint weight = decoder.PixelWidth;
uint destHeight = height > expHeight ? expHeight : height;
uint destWeight = weight> expWidth? expWidth : weight;
BitmapTransform transform = new BitmapTransform()
{
ScaledWidth = destWeight,
ScaledHeight = destHeight
};
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
var tempfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("temp.jpg", CreationCollisionOption.ReplaceExisting);
IRandomAccessStream destStream = await tempfile.OpenAsync(FileAccessMode.ReadWrite);
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destStream);
encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform.ScaledWidth, transform.ScaledHeight, 100, 100, pixelData.DetachPixelData());
await encoder.FlushAsync();
//把流的位置变为0,这样才能从头读出图片流
destStream.Seek(0);
return destStream;
}
示例4: PlayStreamAsync
public static async Task PlayStreamAsync(this MediaElement mediaElement, IRandomAccessStream stream, bool disposeStream = true)
{
// bool is irrelevant here, just using this to flag task completion.
TaskCompletionSource<bool> taskCompleted = new TaskCompletionSource<bool>();
// Note that the MediaElement needs to be in the UI tree for events like MediaEnded to fire.
RoutedEventHandler endOfPlayHandler = (s, e) =>
{
if (disposeStream)
stream.Dispose();
taskCompleted.SetResult(true);
};
mediaElement.MediaEnded += endOfPlayHandler;
mediaElement.SetSource(stream, (stream as SpeechSynthesisStream).ContentType);
mediaElement.Volume = 1;
mediaElement.IsMuted = false;
mediaElement.Play();
await taskCompleted.Task;
mediaElement.MediaEnded -= endOfPlayHandler;
}
示例5: CaptureToStreamAsync
//Creates RenderTargetBitmap from UI Element
async Task<RenderTargetBitmap> CaptureToStreamAsync(FrameworkElement uielement, IRandomAccessStream stream, Guid encoderId)
{
try
{
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(uielement);
var pixels = await renderTargetBitmap.GetPixelsAsync();
var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
logicalDpi,
logicalDpi,
/*pixels.ToArray()*/ null);
await encoder.FlushAsync();
return renderTargetBitmap;
}
catch
{
}
return null;
}
示例6: ImageToBytes
private async Task<byte[]> ImageToBytes(IRandomAccessStream sourceStream)
{
byte[] imageArray;
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);
var transform = new BitmapTransform { ScaledWidth = decoder.PixelWidth, ScaledHeight = decoder.PixelHeight };
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.RespectExifOrientation,
ColorManagementMode.DoNotColorManage);
using (var destinationStream = new InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destinationStream);
encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, decoder.PixelWidth,
decoder.PixelHeight, 96, 96, pixelData.DetachPixelData());
await encoder.FlushAsync();
BitmapDecoder outputDecoder = await BitmapDecoder.CreateAsync(destinationStream);
await destinationStream.FlushAsync();
imageArray = (await outputDecoder.GetPixelDataAsync()).DetachPixelData();
}
return imageArray;
}
示例7: UpdateImage
public void UpdateImage(IRandomAccessStream rass)
{
storage["Picture"] = new ParseFile("Picture.jpg", rass.AsStreamForRead());
this.RaisePropertyChanged("Picture");
}
示例8: InitializeAsync
public async Task<ImageSource> InitializeAsync(CoreDispatcher dispatcher, IRandomAccessStream streamSource, CancellationTokenSource cancellationTokenSource)
{
byte[] bytes = new byte[streamSource.Size];
await streamSource.ReadAsync(bytes.AsBuffer(), (uint)streamSource.Size, InputStreamOptions.None).AsTask(cancellationTokenSource.Token);
var imageSource = WebpCodec.Decode(bytes);
return imageSource;
}
示例9: GetImageFromFile
/// <summary>
/// Returns a Bitmap image for a give random access stream
/// </summary>
/// <param name="stream">Random access stream for which the bitmap needs to be generated</param>
/// <return Type="BitmapImage">Bitmap for the given stream</return>
static async public Task<BitmapImage> GetImageFromFile(IRandomAccessStream stream)
{
BitmapImage bmp = new BitmapImage();
await bmp.SetSourceAsync(stream);
return bmp;
}
示例10: ReadIntoBuffer
public static async Task<IBuffer> ReadIntoBuffer(IRandomAccessStream stream)
{
stream.Seek(0);
var buffer = new Buffer((uint) stream.Size);
await stream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.None);
return buffer;
}
示例11: UploadImgur
public static async Task<ImgurEntity> UploadImgur(IRandomAccessStream fileStream)
{
try
{
var imageData = new byte[fileStream.Size];
for (int i = 0; i < imageData.Length; i++)
{
imageData[i] = (byte)fileStream.AsStreamForRead().ReadByte();
}
var theAuthClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.imgur.com/3/image");
request.Headers.Authorization = new AuthenticationHeaderValue("Client-ID", "e5c018ac1f4c157");
var form = new MultipartFormDataContent();
var t = new StreamContent(fileStream.AsStream());
// TODO: See if this is the correct way to use imgur's v3 api. I can't see why we would still need to convert images to base64.
string base64Img = Convert.ToBase64String(imageData);
t.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
form.Add(new StringContent(base64Img), @"image");
form.Add(new StringContent("file"), "type");
request.Content = form;
HttpResponseMessage response = await theAuthClient.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
if (responseString == null) return null;
var imgurEntity = JsonConvert.DeserializeObject<ImgurEntity>(responseString);
return imgurEntity;
}
catch (WebException)
{
}
catch (IOException)
{
return null;
}
return null;
}
示例12: Decrypt
/// <summary>
/// Decrypts the specified input stream.
/// </summary>
/// <param name="input">The input stream.</param>
/// <param name="masterKey">The master key.</param>
/// <param name="masterSeed">The master seed.</param>
/// <param name="encryptionIV">The encryption initialization vector.</param>
/// <returns>The decrypted buffer.</returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="input"/>, <paramref name="masterSeed"/>, <paramref name="masterKey"/>
/// and <paramref name="encryptionIV"/> cannot be <c>null</c>.
/// </exception>
public static async Task<IInputStream> Decrypt(IRandomAccessStream input,
IBuffer masterKey, IBuffer masterSeed, IBuffer encryptionIV)
{
if (input == null) throw new ArgumentNullException("input");
if (masterSeed == null) throw new ArgumentNullException("masterSeed");
if (masterKey == null) throw new ArgumentNullException("masterKey");
if (encryptionIV == null) throw new ArgumentNullException("encryptionIV");
var sha = HashAlgorithmProvider
.OpenAlgorithm(HashAlgorithmNames.Sha256)
.CreateHash();
sha.Append(masterSeed);
sha.Append(masterKey);
var seed = sha.GetValueAndReset();
var aes = SymmetricKeyAlgorithmProvider
.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7)
.CreateSymmetricKey(seed);
var buffer = WindowsRuntimeBuffer.Create(
(int)(input.Size - input.Position));
buffer = await input.ReadAsync(buffer, buffer.Capacity);
buffer = CryptographicEngine.Decrypt(aes, buffer, encryptionIV);
var stream = new InMemoryRandomAccessStream();
await stream.WriteAsync(buffer);
stream.Seek(0);
return stream;
}
示例13: ConverterBitmapToTargetStreamAsync
public async Task ConverterBitmapToTargetStreamAsync(IRandomAccessStream bitmapSourceStream, IRandomAccessStream saveTargetStream)
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(bitmapSourceStream);
// Scale image to appropriate size
BitmapTransform transform = new BitmapTransform()
{
//ScaledWidth = Convert.ToUInt32(bi.PixelWidth),
//ScaledHeight = Convert.ToUInt32(bi.PixelHeight)
};
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.RespectExifOrientation, // This sample ignores Exif orientation
ColorManagementMode.DoNotColorManage);
var BitmapEncoderGuid = BitmapEncoder.PngEncoderId;
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, saveTargetStream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
(uint)decoder.PixelWidth,
(uint)decoder.PixelHeight,
96.0,
96.0,
pixelData.DetachPixelData());
await encoder.FlushAsync();
}
示例14: GetThumbnailImage
public async Task<IRandomAccessStream> GetThumbnailImage(IRandomAccessStream stream, int width, int height, bool smartCropping = true)
{
var response = await _client.GetThumbnailAsync(stream.AsStreamForRead(), width, height, smartCropping);
var responseStream = new MemoryStream(response);
return responseStream.AsRandomAccessStream();
}
示例15: ResizeImage
/// <summary>
/// 改变图片大小
/// </summary>
/// <param name="sourceStream">包含图片数据的数据流</param>
/// <param name="scaleLong">如果图片长大于宽,那么此为改编后的长度,反之是改变后的高度</param>
/// <returns></returns>
public static async Task<IRandomAccessStream> ResizeImage(IRandomAccessStream sourceStream,uint scaleLong)
{
try
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);
uint height = decoder.PixelHeight;
uint weight = decoder.PixelWidth;
double rate;
uint destHeight = height;
uint destWeight = weight;
if (weight > height)
{
rate = scaleLong / (double)weight;
destHeight = weight > scaleLong ? (uint)(rate * height) : height;
destWeight = scaleLong;
}
else
{
rate = scaleLong / (double)height;
destWeight = height > scaleLong ? (uint)(rate * weight) : weight;
destHeight = scaleLong;
}
BitmapTransform transform = new BitmapTransform()
{
ScaledWidth = destWeight,
ScaledHeight = destHeight
};
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
var folder = ApplicationData.Current.TemporaryFolder;
var tempfile = await folder.CreateFileAsync("temp.jpg", CreationCollisionOption.GenerateUniqueName);
IRandomAccessStream destStream = await tempfile.OpenAsync(FileAccessMode.ReadWrite);
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destStream);
encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform.ScaledWidth, transform.ScaledHeight, 100, 100, pixelData.DetachPixelData());
await encoder.FlushAsync();
//REMEMBER
destStream.Seek(0);
await tempfile.DeleteAsync(StorageDeleteOption.PermanentDelete);
return destStream;
}
catch(Exception e)
{
var task = ExceptionHelper.WriteRecordAsync(e, nameof(BitmapHandleHelper), nameof(ResizeImage));
return null;
}
}