本文整理汇总了C#中Windows.UI.Xaml.Media.Imaging.WriteableBitmap.Invalidate方法的典型用法代码示例。如果您正苦于以下问题:C# WriteableBitmap.Invalidate方法的具体用法?C# WriteableBitmap.Invalidate怎么用?C# WriteableBitmap.Invalidate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.UI.Xaml.Media.Imaging.WriteableBitmap
的用法示例。
在下文中一共展示了WriteableBitmap.Invalidate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToBitmapImageAsync
public unsafe async static Task<WriteableBitmap> ToBitmapImageAsync(this BitmapHolder holder)
{
if (holder == null || holder.Pixels == null)
return null;
var writeableBitmap = new WriteableBitmap(holder.Width, holder.Height);
using (var stream = writeableBitmap.PixelBuffer.AsStream())
{
int length = holder.Pixels.Length;
var buffer = new byte[length * 4];
fixed (int* srcPtr = holder.Pixels)
{
var b = 0;
for (var i = 0; i < length; i++, b += 4)
{
var p = srcPtr[i];
buffer[b + 3] = (byte)((p >> 24) & 0xff);
buffer[b + 2] = (byte)((p >> 16) & 0xff);
buffer[b + 1] = (byte)((p >> 8) & 0xff);
buffer[b + 0] = (byte)(p & 0xff);
}
stream.Write(buffer, 0, length * 4);
}
}
writeableBitmap.Invalidate();
return writeableBitmap;
}
示例2: OnImageUrlChanged
private async void OnImageUrlChanged()
{
if (ImageUrl == null)
{
return;
}
var rass = RandomAccessStreamReference.CreateFromUri(new Uri(ImageUrl, UriKind.Absolute));
IRandomAccessStream stream = await rass.OpenReadAsync();
var decoder = await BitmapDecoder.CreateAsync(stream);
Rect bounds = Window.Current.Bounds;
//WriteableBitmap bmp = new WriteableBitmap((int)bounds.Width, (int)bounds.Height);
var displayWidth = Math.Min(decoder.PixelWidth, bounds.Width);
var displayHeight = Math.Min(decoder.PixelHeight, bounds.Height);
WriteableBitmap bmp = new WriteableBitmap((int)displayWidth, (int)displayHeight);
stream.Seek(0);
//var blurFilter = new BlurFilter(60);
using (var source = new RandomAccessStreamImageSource(stream))
using (var blurFilter = new LensBlurEffect(source, new LensBlurPredefinedKernel(LensBlurPredefinedKernelShape.Circle, 20)))
//using (var filterEffect = new FilterEffect(source) { Filters = new[] { blurFilter } })
using (var renderer = new WriteableBitmapRenderer(blurFilter, bmp))
{
bmp = await renderer.RenderAsync();
bmp.Invalidate();
BackgroundImage.Source = bmp;
}
}
示例3: SelectImageFromPicker
async Task<WriteableBitmap> SelectImageFromPicker()
{
var picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
ImageProperties imgProp = await file.Properties.GetImagePropertiesAsync();
var savedPictureStream = await file.OpenAsync(FileAccessMode.Read);
//set image properties and show the taken photo
bitmap = new WriteableBitmap((int)imgProp.Width, (int)imgProp.Height);
await bitmap.SetSourceAsync(savedPictureStream);
bitmap.Invalidate();
SaveImageAsync(file);
return bitmap;
}
else return null;
}
示例4: LoadImage
private static WriteableBitmap LoadImage(byte[] pixels, int width, int height)
{
var bitmap = new WriteableBitmap(width, height);
var buffer = bitmap.PixelBuffer;
pixels.CopyTo(buffer);
bitmap.Invalidate();
return bitmap;
}
示例5: ToBitmap
public object ToBitmap()
{
var bmp = new WriteableBitmap(Width, Heigth);
// Copy data back
using (var stream = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.AsStream(bmp.PixelBuffer))
{
stream.Write(Pixel, 0, Pixel.Length);
}
bmp.Invalidate();
return bmp;
}
示例6: Copy
/// <summary>
/// Copies the specified source bitmap into a new bitmap.
/// </summary>
/// <param name="source">The source bitmap.</param>
/// <returns></returns>
public static WriteableBitmap Copy(this WriteableBitmap source)
{
source.Invalidate();
var copiedBytes =
new byte[4 * source.PixelWidth * source.PixelHeight];
var inputStream = source.PixelBuffer.AsStream();
inputStream.Seek(0, SeekOrigin.Begin);
inputStream.Read(copiedBytes, 0, copiedBytes.Length);
var target = new WriteableBitmap(source.PixelWidth, source.PixelHeight);
var outputStream = target.PixelBuffer.AsStream();
outputStream.Seek(0, SeekOrigin.Begin);
outputStream.Write(copiedBytes, 0, copiedBytes.Length);
target.Invalidate();
return target;
}
示例7: UpdateBitmap
private void UpdateBitmap()
{
var resolutionScale = DisplayInformation.GetForCurrentView ().ResolutionScale;
var screenScale = (float)resolutionScale / 100.0f;
var width = (int)(ActualWidth * screenScale);
var height = (int)(ActualHeight * screenScale);
if (width == 0 || height == 0)
return;
IntPtr buff = Marshal.AllocCoTaskMem (width * height * 4);
try {
using (var surface = SKSurface.Create (width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul, buff, width * 4)) {
var skcanvas = surface.Canvas;
skcanvas.Scale (screenScale, screenScale);
using (new SKAutoCanvasRestore (skcanvas, true)) {
skiaView.SendDraw (skcanvas);
}
}
var pixels = new byte[width * height * 4];
Marshal.Copy (buff, pixels, 0, pixels.Length);
var bitmap = new WriteableBitmap (width, height);
var stream = bitmap.PixelBuffer.AsStream ();
stream.Seek (0, SeekOrigin.Begin);
stream.Write (pixels, 0, pixels.Length);
bitmap.Invalidate ();
var b = bitmap;
Background = new ImageBrush {
ImageSource = b,
AlignmentX = AlignmentX.Center,
AlignmentY = AlignmentY.Center,
Stretch = Stretch.Fill
};
} finally {
if (buff != IntPtr.Zero) {
Marshal.FreeCoTaskMem (buff);
}
}
}
示例8: ProcessBitmapAsync
/// <summary>
/// Processes the RenderTargetBitmap and outputs the result to the output WriteableBitmap.
/// </summary>
/// <param name="rtb">The RenderTargetBitmap that typically includes a screen grab of the portion of UI.</param>
/// <param name="wb">The WriteableBitmap that the effect output is written to.</param>
/// <param name="pw">The pixel width of both bitmaps.</param>
/// <param name="ph">The pixel height of both bitmaps.</param>
/// <returns>A task that completes when the processing is complete.</returns>
public override async Task ProcessBitmapAsync(RenderTargetBitmap rtb, WriteableBitmap wb, int pw, int ph)
{
//var sw = new Stopwatch();
//sw.Start();
var rtbBuffer = await rtb.GetPixelsAsync();
var rtbPixels = rtbBuffer.GetPixels();
var wbBuffer = wb.PixelBuffer;
var wbPixels = wbBuffer.GetPixels();
// Blur
int radius = 1;
for (int x = 0; x < pw; x++)
for (int y = 0; y < ph; y++)
{
int x1min = Math.Max(0, x - radius);
int x1max = Math.Min(x + radius, pw - 1);
int y1min = Math.Max(0, y - radius);
int y1max = Math.Min(y + radius, ph - 1);
int count = (x1max - x1min + 1) * (y1max - y1min + 1) + 7;
var sum = new int[4];
for (int x1 = x1min; x1 <= x1max; x1++)
for (int y1 = y1min; y1 <= y1max; y1++)
for (int i = 0; i < 4; i++)
sum[i] +=
(x == x1 && y == y1) ?
rtbPixels.Bytes[4 * (y1 * pw + x1) + i] * 8 :
rtbPixels.Bytes[4 * (y1 * pw + x1) + i];
for (int i = 0; i < 4; i++)
wbPixels.Bytes[4 * (y * pw + x) + i] = (byte)(sum[i] / count);
}
wbPixels.UpdateFromBytes();
wb.Invalidate();
//sw.Stop();
//new MessageDialog(sw.ElapsedMilliseconds.ToString()).ShowAsync();
}
示例9: LoadTexture
public async Task LoadTexture(string filename)
{
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var texFile = await folder.GetFileAsync(filename);
var properties = await texFile.Properties.GetImagePropertiesAsync();
WriteableBitmap wbmp = new WriteableBitmap((Int32)properties.Width, (Int32)properties.Height);
await wbmp.LoadAsync(texFile);
byte[] pixels = new byte[wbmp.PixelWidth * wbmp.PixelHeight * 4];
using (Stream pixelStream = wbmp.PixelBuffer.AsStream())
{
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
}
wbmp.Invalidate();
TexturePixels = new Vector3[wbmp.PixelHeight, wbmp.PixelWidth];
int r = 0, c = 0;
for (int i = 0; i < pixels.Length; i++)
{
TexturePixels[r, c] = new Vector3(pixels[i + 2], pixels[i + 1], pixels[i + 0]) / 255.0f;
c = (c + 1) % wbmp.PixelWidth;
if (c == 0)
{
r = (r + 1) % wbmp.PixelHeight;
}
i += 3;
}
TexSize = new Vector2(wbmp.PixelWidth, wbmp.PixelHeight);
}
示例10: ProcessBitmap
/// <summary>
/// Processes the RenderTargetBitmap and outputs the result to the output WriteableBitmap.
/// </summary>
/// <param name="rtb">The RenderTargetBitmap that typically includes a screen grab of the portion of UI.</param>
/// <param name="wb">The WriteableBitmap that the effect output is written to.</param>
/// <param name="pw">The pixel width of both bitmaps.</param>
/// <param name="ph">The pixel height of both bitmaps.</param>
/// <returns>A task that completes when the processing is complete.</returns>
public override async Task ProcessBitmap(RenderTargetBitmap rtb, WriteableBitmap wb, int pw, int ph)
{
var rtbBuffer = await rtb.GetPixelsAsync();
var rtbPixels = rtbBuffer.GetPixels();
var wbBuffer = wb.PixelBuffer;
var wbPixels = wbBuffer.GetPixels();
var r = this.Color.R;
var g = this.Color.G;
var b = this.Color.B;
// Expand
const int expansion = 1;
for (int x = 0; x < pw; x++)
for (int y = 0; y < ph; y++)
{
int x1min = Math.Max(0, x - expansion);
int x1max = Math.Min(x + expansion, pw - 1);
int y1min = Math.Max(0, y - expansion);
int y1max = Math.Min(y + expansion, ph - 1);
byte maxa = 0;
for (int x1 = x1min; x1 <= x1max; x1++)
for (int y1 = y1min; y1 <= y1max; y1++)
{
var a = rtbPixels.Bytes[4 * (y1 * pw + x1) + 3];
if (a > maxa)
maxa = a;
}
wbPixels.Bytes[4 * (y * pw + x)] = b;
wbPixels.Bytes[4 * (y * pw + x) + 1] = g;
wbPixels.Bytes[4 * (y * pw + x) + 2] = r;
wbPixels.Bytes[4 * (y * pw + x) + 3] = maxa;
}
wbPixels.UpdateFromBytes();
wb.Invalidate();
}
示例11: Crop
public static WriteableBitmap Crop(this WriteableBitmap source, int x1, int y1, int x2, int y2)
{
if (x1 >= x2 ||
y1 >= y2 ||
x1 < 0 ||
y1 < 0 ||
x2 < 0 ||
y2 < 0 ||
x1 > source.PixelWidth ||
y1 > source.PixelHeight ||
x2 > source.PixelWidth ||
y2 > source.PixelHeight)
{
throw new ArgumentException();
}
//var buffer = source.PixelBuffer.GetPixels();
var cw = x2 - x1;
var ch = y2 - y1;
var target = new WriteableBitmap(cw, ch);
var croppedBytes =
new byte[4 * cw * ch];
var inputStream = source.PixelBuffer.AsStream();
inputStream.Seek(4 * (source.PixelWidth * y1 + x1), SeekOrigin.Current);
for (int i = 0; i < ch; i++)
{
inputStream.Read(croppedBytes, 4 * cw * i, 4 * cw);
inputStream.Seek(4 * (source.PixelWidth - cw), SeekOrigin.Current);
}
var outputStream = target.PixelBuffer.AsStream();
outputStream.Seek(0, SeekOrigin.Begin);
outputStream.Write(croppedBytes, 0, croppedBytes.Length);
target.Invalidate();
return target;
}
示例12: LoadBitmap
private async Task LoadBitmap(IRandomAccessStream stream)
{
_writeableBitmap = new WriteableBitmap(1, 1);
_writeableBitmap.SetSource(stream);
_writeableBitmap.Invalidate();
await Dispatcher.RunAsync(
Windows.UI.Core.CoreDispatcherPriority.Normal,
() => ImageTarget.Source = _writeableBitmap);
}
示例13: OnThumbnailComplete
private static void OnThumbnailComplete(IImageProcessor processor, SoftwareBitmap softwareBitmap)
{
// Note: intentionally not awaited. We just deliver the thumbnail Bitmaps to the UI thread.
TaskUtilities.RunOnDispatcherThreadAsync(() =>
{
WriteableBitmap writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight);
softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer);
writeableBitmap.Invalidate();
processor.ThumbnailImagSource = writeableBitmap;
// The thumbnail image has been copied, so we can destroy the Bitmap.
softwareBitmap.Dispose();
});
}
示例14: RenderToWriteableBitmap
public async Task<WriteableBitmap> RenderToWriteableBitmap(FrameworkElement fe)
{
var width = (int)Math.Ceiling(fe.ActualWidth);
var height = (int)Math.Ceiling(fe.ActualHeight);
if (width == 0 ||
height == 0)
{
throw new InvalidOperationException("Can't render an empty element. ActualWidth or ActualHeight equal 0. Consider awaiting a WaitForNonZeroSizeAsync() call or invoking Measure()/Arrange() before the call to Render().");
}
using (var renderTargetBitmap = CreateRenderTargetBitmap(width, height))
{
_d2DDeviceContext.Target = renderTargetBitmap;
_d2DDeviceContext.AntialiasMode = D2D.AntialiasMode.PerPrimitive;
_d2DDeviceContext.TextAntialiasMode = D2D.TextAntialiasMode.Grayscale;
await Compose(_d2DDeviceContext, fe);
using (var cpuReadBitmap = CreateCpuReadBitmap(width, height))
{
cpuReadBitmap.CopyFromRenderTarget(
_d2DDeviceContext,
new Point(0, 0),
new SharpDX.Rectangle(0, 0, width, height));
var mappedRect = cpuReadBitmap.Map(D2D.MapOptions.Read);
try
{
using (var readStream =
new DataStream(
userBuffer: mappedRect.DataPointer,
sizeInBytes: mappedRect.Pitch * height,
canRead: true,
canWrite: false))
{
var wb = new WriteableBitmap(width, height);
using (var writeStream = wb.PixelBuffer.AsStream())
{
var buffer = new byte[mappedRect.Pitch];
for (int i = 0; i < height; i++)
{
readStream.Read(buffer, 0, mappedRect.Pitch);
writeStream.Write(buffer, 0, width * 4);
}
}
wb.Invalidate();
return wb;
}
}
finally
{
cpuReadBitmap.Unmap();
}
}
}
}
示例15: LoadWallpaperArt
private async void LoadWallpaperArt()
{
if (_loaded ||
!App.Locator.AppSettingsHelper.Read("WallpaperArt", true, SettingsStrategy.Roaming)) return;
var wait = App.Locator.AppSettingsHelper.Read<int>("WallpaperDayWait");
var created = App.Locator.AppSettingsHelper.ReadJsonAs<DateTime>("WallpaperCreated");
// Set the image brush
var imageBrush = new ImageBrush { Opacity = .25, Stretch = Stretch.UniformToFill, AlignmentY = AlignmentY.Top};
LayoutGrid.Background = imageBrush;
// Once a week remake the wallpaper
if ((DateTime.Now - created).TotalDays > wait)
{
var albums =
App.Locator.CollectionService.Albums.ToList()
.Where(p => p.HasArtwork)
.ToList();
var albumCount = albums.Count;
if (albumCount < 10) return;
var h = Window.Current.Bounds.Height;
var rows = (int) Math.Ceiling(h/(ActualWidth/5));
const int collumns = 5;
var albumSize = (int) Window.Current.Bounds.Width/collumns;
var numImages = rows*5;
var imagesNeeded = numImages - albumCount;
var shuffle = await Task.FromResult(albums
.Shuffle()
.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));
//.........这里部分代码省略.........