本文整理汇总了C#中Windows.UI.Xaml.Media.Imaging.WriteableBitmap.ToStreamAsJpeg方法的典型用法代码示例。如果您正苦于以下问题:C# WriteableBitmap.ToStreamAsJpeg方法的具体用法?C# WriteableBitmap.ToStreamAsJpeg怎么用?C# WriteableBitmap.ToStreamAsJpeg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.UI.Xaml.Media.Imaging.WriteableBitmap
的用法示例。
在下文中一共展示了WriteableBitmap.ToStreamAsJpeg方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveImageAsync
private async Task SaveImageAsync(WriteableBitmap bitmap)
{
var folder = KnownFolders.PicturesLibrary;
var file = await folder.CreateFileAsync("placa.jpeg", CreationCollisionOption.GenerateUniqueName);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await bitmap.ToStreamAsJpeg(stream);
}
}
示例2: GetPixels
private static async Task<Color> GetPixels(WriteableBitmap bitmap, Color[] pixels, Int32 width, Int32 height)
{
IRandomAccessStream bitmapStream = new InMemoryRandomAccessStream();
await bitmap.ToStreamAsJpeg(bitmapStream);
var bitmapDecoder = await BitmapDecoder.CreateAsync(bitmapStream);
var pixelProvider = await bitmapDecoder.GetPixelDataAsync();
Byte[] byteArray = pixelProvider.DetachPixelData();
Int32 r = 0, g = 0, b = 0;
int sum = pixels.Length;
for (var i = 0; i < height; i++)
{
for (var j = 0; j < width; j++)
{
r += byteArray[(i * width + j) * 4 + 2];
g += byteArray[(i * width + j) * 4 + 1];
b += byteArray[(i * width + j) * 4 + 0];
}
}
return Color.FromArgb((byte)(255), (byte)(r / sum), (byte)(g / sum), (byte)(b / sum));
}
示例3: GetPixels
public static async Task GetPixels(WriteableBitmap bitmap, Color[] pixels, Int32 width, Int32 height)
{
IRandomAccessStream bitmapStream = new InMemoryRandomAccessStream();
await bitmap.ToStreamAsJpeg(bitmapStream);
var bitmapDecoder = await BitmapDecoder.CreateAsync(bitmapStream);
var pixelProvider = await bitmapDecoder.GetPixelDataAsync();
Byte[] byteArray = pixelProvider.DetachPixelData();
for (var i = 0; i < height; i++)
{
for (var j = 0; j < width; j++)
{
Int32 r = byteArray[(i * height + j) * 4 + 2];
Int32 g = byteArray[(i * height + j) * 4 + 1];
Int32 b = byteArray[(i * height + j) * 4 + 0];
pixels[(i * height + j)] = Color.FromArgb(0xFF, (byte)r, (byte)g, (byte)b);
}
}
}
示例4: LoadWallpaperArt
//.........这里部分代码省略.........
.Take(numImages > albumCount ? albumCount : numImages)
.ToList());
if (imagesNeeded > 0)
{
var repeatList = new List<Album>();
while (imagesNeeded > 0)
{
var takeAmmount = imagesNeeded > albumCount ? albumCount : imagesNeeded;
await Task.Run(() => repeatList.AddRange(shuffle.Shuffle().Take(takeAmmount)));
imagesNeeded -= shuffle.Count;
}
shuffle.AddRange(repeatList);
}
// Initialize an empty WriteableBitmap.
var destination = new WriteableBitmap((int) Window.Current.Bounds.Width,
(int) Window.Current.Bounds.Height);
var col = 0; // Current Column Position
var row = 0; // Current Row Position
destination.Clear(Colors.Black); // Set the background color of the image to black
// will be copied
foreach (var artworkPath in shuffle.Select(album => string.Format(AppConstant.ArtworkPath, album.Id)))
{
var file = await WinRtStorageHelper.GetFileAsync(artworkPath);
// Read the image file into a RandomAccessStream
using (var fileStream = await file.OpenReadAsync())
{
try
{
// Now that you have the raw bytes, create a Image Decoder
var decoder = await BitmapDecoder.CreateAsync(fileStream);
// Get the first frame from the decoder because we are picking an image
var frame = await decoder.GetFrameAsync(0);
// Convert the frame into pixels
var pixelProvider = await frame.GetPixelDataAsync();
// Convert pixels into byte array
var srcPixels = pixelProvider.DetachPixelData();
var wid = (int) frame.PixelWidth;
var hgt = (int) frame.PixelHeight;
// Create an in memory WriteableBitmap of the same size
var bitmap = new WriteableBitmap(wid, hgt); // Temporary bitmap into which the source
using (var pixelStream = bitmap.PixelBuffer.AsStream())
{
pixelStream.Seek(0, SeekOrigin.Begin);
// Push the pixels from the original file into the in-memory bitmap
await pixelStream.WriteAsync(srcPixels, 0, srcPixels.Length);
bitmap.Invalidate();
// Resize the in-memory bitmap and Blit (paste) it at the correct tile
// position (row, col)
destination.Blit(new Rect(col*albumSize, row*albumSize, albumSize, albumSize),
bitmap.Resize(albumSize, albumSize, WriteableBitmapExtensions.Interpolation.Bilinear),
new Rect(0, 0, albumSize, albumSize));
col++;
if (col < collumns) continue;
row++;
col = 0;
}
}
catch
{
// ignored
}
}
}
var wallpaper =
await WinRtStorageHelper.CreateFileAsync("wallpaper.jpg", ApplicationData.Current.LocalFolder);
using (var rndWrite = await wallpaper.OpenAsync(FileAccessMode.ReadWrite))
{
await destination.ToStreamAsJpeg(rndWrite);
}
App.Locator.AppSettingsHelper.WriteAsJson("WallpaperCreated", DateTime.Now);
// If there are 30 or less albums then recreate in one day, else wait a week
App.Locator.AppSettingsHelper.Write("WallpaperDayWait", albums.Count < 30 ? 1 : 7);
imageBrush.ImageSource = null;
imageBrush.ImageSource = new BitmapImage(new Uri("ms-appdata:/local/wallpaper.jpg"));
}
else if (created != DateTime.MinValue)
{
// Not the first time, so there must already be one created
imageBrush.ImageSource = new BitmapImage(new Uri("ms-appdata:/local/wallpaper.jpg"));
}
_loaded = true;
}