本文整理汇总了C#中System.Windows.Media.Imaging.WriteableBitmap.Lock方法的典型用法代码示例。如果您正苦于以下问题:C# WriteableBitmap.Lock方法的具体用法?C# WriteableBitmap.Lock怎么用?C# WriteableBitmap.Lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.WriteableBitmap
的用法示例。
在下文中一共展示了WriteableBitmap.Lock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateImage
public unsafe void CreateImage(WriteableBitmap target, IntPtr pointer)
{
Int32Rect rectangle = default(Int32Rect);
target.Dispatcher.Invoke(new Action(() =>
{
rectangle = new Int32Rect(0, 0, target.PixelWidth, target.PixelHeight);
}));
this.CreateHistogram(pointer, rectangle.Width, rectangle.Height);
var pixelcount = rectangle.Width * rectangle.Height;
var buffer = new byte[pixelcount * 3];
try
{
ushort* pDepth = (ushort*)pointer;
for (int index = 0; index < pixelcount; index++)
{
byte pixel = (byte)histogram.GetValue(*pDepth);
buffer[index * 3] = pixel;
buffer[index * 3 + 1] = pixel;
buffer[index * 3 + 2] = pixel;
pDepth++;
}
}
catch (AccessViolationException)
{ }
catch (SEHException)
{ }
target.Dispatcher.Invoke(new Action(() =>
{
target.Lock();
target.WritePixels(rectangle, buffer, rectangle.Width * 3, 0);
target.Unlock();
}));
}
示例2: UpdateNormalBitmap
public override void UpdateNormalBitmap(WriteableBitmap bitmap, Color color) {
unsafe {
bitmap.Lock();
int currentPixel = -1;
byte* pStart = (byte*)(void*)bitmap.BackBuffer;
double iRowUnit = (double)1 / 256;
double iRowCurrent = 1;
double hue = sModel.HComponent(color);
double saturation = sModel.SComponent(color);
for (int iRow = 0; iRow < bitmap.PixelHeight; iRow++) {
Color hueColor = sModel.Color(hue, saturation, iRowCurrent);
for (int iCol = 0; iCol < bitmap.PixelWidth; iCol++) {
currentPixel++;
*(pStart + currentPixel * 3 + 0) = hueColor.B; //Blue
*(pStart + currentPixel * 3 + 1) = hueColor.G; //Green
*(pStart + currentPixel * 3 + 2) = hueColor.R; //red
}
iRowCurrent -= iRowUnit;
}
bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight));
bitmap.Unlock();
}
}
示例3: MarkFrequency
//public void MarkFrequency(float frequency, WriteableBitmap output)
//{
// if(this.soundSpectrum != null)
// {
// var renderAreaHeight = output.PixelHeight;
// var renderAreaWidth = output.PixelWidth;
// var slotIndex = (int)(Math.Round(((frequency - this.spectrumStartFrequency) / (this.spectrumEndFrequency - this.spectrumStartFrequency)) * renderAreaWidth));
// if(slotIndex < output.PixelWidth && slotIndex >= 0)
// {
// output.Lock();
// this.RenderAtSlot(slotIndex, 1.0f, output, Colors.Red);
// output.AddDirtyRect(new Int32Rect(slotIndex, 0, 1, renderAreaHeight));
// output.Unlock();
// }
// }
//}
public void MarkFrequency(float frequency, WriteableBitmap output)
{
if (this.soundSpectrum != null)
{
var renderAreaHeight = output.PixelHeight;
var renderAreaWidth = output.PixelWidth;
var diff = this.spectrumEndFrequency - this.spectrumStartFrequency;
//var slotIndex = (int)(((frequency - this.spectrumStartFrequency) / diff) * renderAreaWidth);
var slotIndex = (int)Math.Round(frequency / binFrequency);
var startBin = (int)Math.Round(this.spectrumStartFrequency / binFrequency);
slotIndex -= startBin;
slotIndex = (int)Math.Round(((float)slotIndex / this.soundSpectrum.Length) * renderAreaWidth);
if (slotIndex < output.PixelWidth && slotIndex >= 0)
{
output.Lock();
this.RenderAtSlot(slotIndex, 1.0f, output, Colors.Red);
output.AddDirtyRect(new Int32Rect(0, 0, renderAreaWidth, renderAreaHeight));
//output.AddDirtyRect(new Int32Rect(slotIndex, 0, 1, renderAreaHeight));
output.Unlock();
}
}
}
示例4: OnRender
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (sample == null)
return;
var m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
var dpiX = m.M11;
var dpiY = m.M22;
int width = (int)(ActualWidth * dpiX);
int height = (int)(ActualHeight * dpiY);
var bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);
bitmap.Lock();
using (var surface = SKSurface.Create(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul, bitmap.BackBuffer, bitmap.BackBufferStride))
{
var skcanvas = surface.Canvas;
skcanvas.Scale((float)dpiX, (float)dpiY);
using (new SKAutoCanvasRestore(skcanvas, true))
{
sample.Method(skcanvas, (int)ActualWidth, (int)ActualHeight);
}
}
bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
bitmap.Unlock();
drawingContext.DrawImage(bitmap, new Rect(0, 0, ActualWidth, ActualHeight));
}
示例5: ToGrayScale
private static unsafe BitmapSource ToGrayScale(BitmapSource source)
{
const int PIXEL_SIZE = 4;
int width = source.PixelWidth;
int height = source.PixelHeight;
var bitmap = new WriteableBitmap(source);
bitmap.Lock();
var backBuffer = (byte*)bitmap.BackBuffer.ToPointer();
for (int y = 0; y < height; y++)
{
var row = backBuffer + (y * bitmap.BackBufferStride);
for (int x = 0; x < width; x++)
{
var grayScale = (byte)(((row[x * PIXEL_SIZE + 1]) + (row[x * PIXEL_SIZE + 2]) + (row[x * PIXEL_SIZE + 3])) / 3);
for (int i = 0; i < PIXEL_SIZE; i++)
row[x * PIXEL_SIZE + i] = grayScale;
}
}
bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
bitmap.Unlock();
return bitmap;
}
示例6: DrawPixel
public virtual void DrawPixel(ref WriteableBitmap writeableBitmap, int x, int y)
{
int column = x;
int row = y;
// Reserve the back buffer for updates.
writeableBitmap.Lock();
unsafe
{
// Get a pointer to the back buffer.
int pBackBuffer = (int)writeableBitmap.BackBuffer;
// Find the address of the pixel to draw.
pBackBuffer += row * writeableBitmap.BackBufferStride;
pBackBuffer += column * 4;
// Compute the pixel's color.
int color_data = 255 << 16; // R
color_data |= 255 << 8; // G
color_data |= 255 << 0; // B
// Assign the color data to the pixel.
*((int*)pBackBuffer) = color_data;
}
// Specify the area of the bitmap that changed.
writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));
// Release the back buffer and make it available for display.
writeableBitmap.Unlock();
}
示例7: CreateImage
public unsafe void CreateImage(WriteableBitmap target, IntPtr pointer)
{
Int32Rect rectangle = default(Int32Rect);
target.Dispatcher.Invoke(new Action(() =>
{
rectangle = new Int32Rect(0, 0, target.PixelWidth, target.PixelHeight);
}));
byte* pImage = (byte*)pointer.ToPointer();
var pixelCount = rectangle.Width * rectangle.Height;
var buffer = new byte[pixelCount * 3];
for (int index = 0; index < pixelCount; index++)
{
buffer[index * 3] = pImage[2];
buffer[index * 3 + 1] = pImage[1];
buffer[index * 3 + 2] = pImage[0];
pImage += 3;
}
target.Dispatcher.Invoke(new Action(() =>
{
target.Lock();
target.WritePixels(rectangle, buffer, rectangle.Width * 3, 0);
target.Unlock();
}));
}
示例8: PaintGray16
private void PaintGray16(WriteableBitmap b, DepthMetaData depthMeta)
{
b.Lock();
short* pDepthRow = (short*) depthMeta.DepthMapPtr;
int nTexMapX = b.BackBufferStride / (b.Format.BitsPerPixel / 8);
short* pTexRow = (short*) b.BackBuffer + depthMeta.YOffset*nTexMapX;
for (int y = 0; y < depthMeta.YRes; y++)
{
short* pDepth = pDepthRow;
short* pTex = pTexRow + depthMeta.XOffset;
for (int x = 0; x < depthMeta.XRes; x++)
{
if (*pDepth != 0)
{
*pTex = (short) _depthHist[*pDepth];
}
else
{
*pTex = 0;
}
pDepth++;
pTex++;
}
pDepthRow += depthMeta.XRes;
pTexRow += nTexMapX;
}
b.AddDirtyRect(new Int32Rect(0, 0, b.PixelWidth, b.PixelHeight));
b.Unlock();
}
示例9: DissolveImages
/// <summary>
/// Dissolves two images using alphablending
/// </summary>
/// <param name="startImage">Image for percentage=0</param>
/// <param name="endImage">Image for percentage=1</param>
/// <param name="morphingProgress">Dissolve percentage from 0 to 1</param>
/// <param name="outputImage">Target for image output data</param>
public unsafe void DissolveImages(Morphing.ImageData startImage, Morphing.ImageData endImage, float percentage, WriteableBitmap outputImage)
{
System.Diagnostics.Debug.Assert(percentage >= 0.0f && percentage <= 1.0f);
System.Diagnostics.Debug.Assert(startImage != null && endImage != null && outputImage != null);
outputImage.Lock();
int width = outputImage.PixelWidth;
int height = outputImage.PixelHeight;
float xStep = 1.0f / width;
Color* outputData = (Color*)outputImage.BackBuffer;
Parallel.For(0, outputImage.PixelHeight, yi =>
{
Color* outputDataPixel = outputData + yi * width;
Color* lastOutputDataPixel = outputDataPixel + width;
float y = (float)yi / height;
for (float x = 0; outputDataPixel != lastOutputDataPixel; x += xStep, ++outputDataPixel)
{
*outputDataPixel = Color.Lerp(startImage.Sample(x, y), endImage.Sample(x, y), percentage);
}
});
outputImage.AddDirtyRect(new System.Windows.Int32Rect(0, 0, outputImage.PixelWidth, outputImage.PixelHeight));
outputImage.Unlock();
}
示例10: Main
public static void Main(string[] args)
{
Console.WriteLine("");
if (args.Length < 2) usage();
mapObj map = new mapObj(args[0]);
Console.WriteLine("# Map layers " + map.numlayers + "; Map name = " + map.name);
for (int i = 0; i < map.numlayers; i++)
{
Console.WriteLine("Layer [" + i + "] name: " + map.getLayer(i).name);
}
try
{
WriteableBitmap mapImage = new WriteableBitmap(map.width, map.height, 96, 96, PixelFormats.Bgr32, null);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (imageObj image = map.draw())
{
// Reserve the back buffer for updates.
mapImage.Lock();
try
{
if (image.getRawPixels(mapImage.BackBuffer) == (int)MS_RETURN_VALUE.MS_FAILURE)
{
Console.WriteLine("Unable to get image contents");
}
// Specify the area of the bitmap that changed.
mapImage.AddDirtyRect(new Int32Rect(0, 0, map.width, map.height));
}
finally
{
// Release the back buffer and make it available for display.
mapImage.Unlock();
}
Console.WriteLine("Rendering time: " + stopwatch.ElapsedMilliseconds + "ms");
// Save the bitmap into a file.
using (FileStream stream = new FileStream(args[1], FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(mapImage));
encoder.Save(stream);
}
}
}
catch (Exception ex)
{
Console.WriteLine( "\nMessage ---\n{0}", ex.Message );
Console.WriteLine(
"\nHelpLink ---\n{0}", ex.HelpLink );
Console.WriteLine( "\nSource ---\n{0}", ex.Source );
Console.WriteLine(
"\nStackTrace ---\n{0}", ex.StackTrace );
Console.WriteLine(
"\nTargetSite ---\n{0}", ex.TargetSite ); }
}
示例11: CharacterBitmap
public CharacterBitmap(byte[] data)
{
//Load the data into the byte array
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Character[j] = data[i];
i++;
}
}
//Now generate a bitmap somehow
CharacterImage = new WriteableBitmap(8, 8, 96, 96, PixelFormats.Bgr32, null);
CharacterImage.Lock();
for(int i = 0; i < 8; i++)
{
if((Character[i] & 0x10) == 0x10)
{
CharacterImage.SetPixel(3, i, Colors.White);
} else
{
CharacterImage.SetPixel(3, i, Colors.Black);
}
if ((Character[i] & 0x08) == 0x08)
{
CharacterImage.SetPixel(4, i, Colors.White);
}
else
{
CharacterImage.SetPixel(4, i, Colors.Black);
}
if ((Character[i] & 0x04) == 0x04)
{
CharacterImage.SetPixel(5, i, Colors.White);
}
else
{
CharacterImage.SetPixel(5, i, Colors.Black);
}
if ((Character[i] & 0x02) == 0x02)
{
CharacterImage.SetPixel(6, i, Colors.White);
}
else
{
CharacterImage.SetPixel(6, i, Colors.Black);
}
if ((Character[i] & 0x01) == 0x01)
{
CharacterImage.SetPixel(7, i, Colors.White);
}
else
{
CharacterImage.SetPixel(7, i, Colors.Black);
}
}
CharacterImage.Unlock();
}
示例12: Window1
public Window1()
{
InitializeComponent();
SnapsToDevicePixels = true;
SizeToContent = SizeToContent.WidthAndHeight;
var i = new Image
{
Width = MandelbrotProvider.DefaultWidth,
Height = MandelbrotProvider.DefaultHeight
};
var s = new WriteableBitmap(MandelbrotProvider.DefaultWidth, MandelbrotProvider.DefaultHeight,
// dpi does not seem to matter at this point
96, 96, PixelFormats.Pbgra32, null);
var shift = 0;
Action Refresh =
delegate
{
var buffer = MandelbrotProvider.DrawMandelbrotSet(shift);
s.Lock();
for (int j = 0; j < buffer.Length; j++)
{
Marshal.WriteInt32(s.BackBuffer, j * 4, unchecked((int)((uint)buffer[j] | 0xff000000)));
}
s.AddDirtyRect(new Int32Rect(0, 0, MandelbrotProvider.DefaultWidth, MandelbrotProvider.DefaultHeight));
s.Unlock();
};
var t = new DispatcherTimer();
t.Tick +=
delegate
{
shift++;
Refresh();
};
t.Interval = TimeSpan.FromMilliseconds(10);
t.Start();
Refresh();
i.Source = s;
this.Content = i;
}
示例13: MainPage
public MainPage()
{
InitializeComponent();
this.Width = 600;
this.Height = 600;
var i = new Image();
//var s = new WriteableBitmap(600, 600, 96, 96, PixelFormats.Pbgra32, null);
var s = new WriteableBitmap(600, 600, PixelFormats.Pbgra32);
Plasma.generatePlasma(600, 600);
var shift = 0;
Action Refresh =
delegate
{
var buffer = Plasma.shiftPlasma(shift);
s.Lock();
for (int j = 0; j < buffer.Length; j++)
{
//Marshal.WriteInt32(s.BackBuffer, j * 4, unchecked((int)(buffer[j] | 0xff000000)));
s[j] = unchecked((int)(buffer[j] | 0xff000000));
}
//s.AddDirtyRect(new Int32Rect(0, 0, 600, 600));
s.Invalidate();
s.Unlock();
};
var t = new DispatcherTimer();
t.Tick +=
delegate
{
shift++;
Refresh();
};
t.Interval = TimeSpan.FromMilliseconds(50);
t.Start();
Refresh();
i.Source = s;
this.Content = i;
}
示例14: LoadImageFromExtraImage
public BitmapSource LoadImageFromExtraImage(ExtraImage item, BitmapSource extraImage)
{
var image = new WriteableBitmap(item.Width, item.Height, 96, 96, PixelFormats.Pbgra32, null);
var rect = new Int32Rect(item.X, item.Y, item.Width, item.Height);
image.Lock();
extraImage.CopyPixels(rect, image.BackBuffer, image.BackBufferStride * image.PixelHeight, image.BackBufferStride);
image.Unlock();
image.Freeze();
return image;
}
示例15: GetImage
public static WriteableBitmap GetImage(string path, int width, int height)
{
// Y,Cb,Crのbyte列を取得
byte[] lum, cb, cr;
int ret;
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// byte列の確保
lum = new byte[fs.Length / 2];
cb = new byte[lum.Length / 2];
cr = new byte[lum.Length / 2];
// 読込み
ret = fs.Read(lum, 0, lum.Length);
ret = fs.Read(cr, 0, cr.Length);
ret = fs.Read(cb, 0, cb.Length);
}
double temp;
WriteableBitmap wbmp = new WriteableBitmap(
width, height, 96, 96, PixelFormats.Bgr32, null);
wbmp.Lock();
byte[] buf = new byte[wbmp.PixelWidth * wbmp.PixelHeight * 4];
for (int i = 0; i < buf.Length; i += 4)
{
int index = i / 4;
// B
temp = 1.164 * (lum[index] - 16) + 2.018 * (cr[index / 2] - 128);
if (temp > 255) temp = 255;
if (temp < 0) temp = 0;
buf[i] = (byte)temp;
// G
temp = 1.164 * (lum[index] - 16) - 0.391 * (cr[index / 2] - 128) - 0.813 * (cb[index / 2] - 128);
if (temp > 255) temp = 255;
if (temp < 0) temp = 0;
buf[i + 1] = (byte)temp;
// R
temp = 1.164 * (lum[index] - 16) + 1.596 * (cb[index / 2] - 128);
if (temp > 255) temp = 255;
if (temp < 0) temp = 0;
buf[i + 2] = (byte)temp;
}
Marshal.Copy(buf, 0, wbmp.BackBuffer, buf.Length);
wbmp.AddDirtyRect(new Int32Rect(0, 0, wbmp.PixelWidth, wbmp.PixelHeight));
wbmp.Unlock();
return wbmp;
}