本文整理汇总了C#中System.Windows.Media.Imaging.WriteableBitmap.GetBitmapContext方法的典型用法代码示例。如果您正苦于以下问题:C# WriteableBitmap.GetBitmapContext方法的具体用法?C# WriteableBitmap.GetBitmapContext怎么用?C# WriteableBitmap.GetBitmapContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.WriteableBitmap
的用法示例。
在下文中一共展示了WriteableBitmap.GetBitmapContext方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Button_Click
/// <summary>
/// Randomise the layout of points.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
// Create a new bitmap and set it as our canvas background.
pBitmap = BitmapFactory.New((int)cnvPoints.ActualWidth, (int)cnvPoints.ActualHeight);
var pBrush = new ImageBrush();
pBrush.ImageSource = pBitmap;
cnvPoints.Background = pBrush;
// Clear the bitmap to light blue.
using (pBitmap.GetBitmapContext())
pBitmap.Clear(Colors.LightBlue);
// Get the number we want to generate and update the UI.
var iResult = 0;
if (!int.TryParse(txtPoints.Text, out iResult))
{
txtPoints.Foreground = Brushes.Red;
return;
}
if (iResult < 0)
{
txtPoints.Foreground = Brushes.Red;
return;
}
txtPoints.Foreground = Brushes.Black;
// Clear the tree and canvas.
cnvPoints.Children.Clear();
pTree = new KDTree.KDTree<EllipseWrapper>(2);
// Create a list of points and draw a ghost ellipse for each one.
using (pBitmap.GetBitmapContext())
{
// Generate X new random items.
var pRandom = new Random();
for (int i = 0; i < iResult; ++i)
{
// Position it and add it to the canvas.
var x = pRandom.NextDouble() * cnvPoints.ActualWidth;
var y = pRandom.NextDouble() * cnvPoints.ActualHeight;
// Add it to the tree.
pTree.AddPoint(new double[] { x, y }, new EllipseWrapper(x, y));
// Draw a ghost visual for it.
//pBitmap.DrawEllipse((int)x - 2, (int)y - 2, (int)x + 2, (int)y + 2, Colors.Green);
pBitmap.DrawEllipse((int)x - 2, (int)y - 2, (int)x + 2, (int)y + 2, Colors.Orange);
}
}
}
示例2: MapToBitmap
private WriteableBitmap MapToBitmap(RobotMap map)
{
var img = new WriteableBitmap(map.Cells.GetLength(0), map.Cells.GetLength(1));
var cells = map.Cells;
var min = map.MinMaxPotential.Item1;
var max = map.MinMaxPotential.Item2;
MapCanvas.Children.Clear();
MapCanvas.Children.Add(MapImage);
using (var ctx = img.GetBitmapContext(ReadWriteMode.ReadWrite))
{
var width = img.PixelWidth;
for (int x = 0; x < img.PixelWidth; x++)
{
for (int y = 0; y < img.PixelHeight; y++)
{
var cell = cells[x, y];
int color = BLACK;
if (cell.IsPotential)
{
var pot = ((Cell.Potential)cell).Item;
color = GiveRainbowColor(min, max, pot);
//var text = new TextBlock() { Text = pot.ToString() };
//MapCanvas.Children.Add(text);
//Canvas.SetLeft(text, x * SCALE_FACTOR);
//Canvas.SetTop(text, y * SCALE_FACTOR);
}
else if (cell.IsFree)
color = GREY;
else if (cell.IsGoal)
color = RED;
else if (cell.IsStart)
color = GREEN;
else if (cell.IsObstacle)
color = BLACK;
ctx.Pixels[x + y * width] = color;
}
}
}
return img;
}
示例3: Highlight
public static unsafe WriteableBitmap Highlight(WriteableBitmap bitmap, bool under, bool over)
{
if (!under && !over)
return bitmap;
int color1 = ConvertColor(Colors.Blue);
int color2 = ConvertColor(Colors.Red);
int treshold = 2;
using (BitmapContext bitmapContext = bitmap.GetBitmapContext())
{
for (var i = 0; i < bitmapContext.Width * bitmapContext.Height; i++)
{
int num1 = bitmapContext.Pixels[i];
byte a = (byte)(num1 >> 24);
int num2 = (int)a;
if (num2 == 0)
num2 = 1;
int num3 = 65280 / num2;
//Color col = Color.FromArgb(a, (byte)((num1 >> 16 & (int)byte.MaxValue) * num3 >> 8),
// (byte)((num1 >> 8 & (int)byte.MaxValue) * num3 >> 8),
// (byte)((num1 & (int)byte.MaxValue) * num3 >> 8));
byte R = (byte)((num1 >> 16 & byte.MaxValue) * num3 >> 8);
byte G = (byte)((num1 >> 8 & byte.MaxValue) * num3 >> 8);
byte B = (byte)((num1 & byte.MaxValue) * num3 >> 8);
if (under && R < treshold && G < treshold && B < treshold)
bitmapContext.Pixels[i] = color1;
if (over && R > 255 - treshold && G > 255 - treshold && B > 255 - treshold)
bitmapContext.Pixels[i] = color2;
}
}
return bitmap;
}
示例4: LoadHistogram
private unsafe void LoadHistogram(FileItem fileItem, WriteableBitmap bitmap)
{
fileItem.FileInfo.HistogramBlue = new int[256];
fileItem.FileInfo.HistogramGreen = new int[256];
fileItem.FileInfo.HistogramRed = new int[256];
fileItem.FileInfo.HistogramLuminance = new int[256];
using (BitmapContext bitmapContext = bitmap.GetBitmapContext())
{
for (var i = 0; i < bitmapContext.Width*bitmapContext.Height; i++)
{
int num1 = bitmapContext.Pixels[i];
byte a = (byte) (num1 >> 24);
int num2 = (int) a;
if (num2 == 0)
num2 = 1;
int num3 = 65280/num2;
byte R = (byte) ((num1 >> 16 & (int) byte.MaxValue)*num3 >> 8);
byte G = (byte) ((num1 >> 8 & (int) byte.MaxValue)*num3 >> 8);
byte B = (byte) ((num1 & (int) byte.MaxValue)*num3 >> 8);
fileItem.FileInfo.HistogramBlue[B]++;
fileItem.FileInfo.HistogramGreen[G]++;
fileItem.FileInfo.HistogramRed[R]++;
int lum = (R + R + R + B + G + G + G + G) >> 3;
fileItem.FileInfo.HistogramLuminance[lum]++;
}
}
//fileItem.FileInfo.HistogramBlue = SmoothHistogram(fileItem.FileInfo.HistogramBlue);
//fileItem.FileInfo.HistogramGreen = SmoothHistogram(fileItem.FileInfo.HistogramGreen);
//fileItem.FileInfo.HistogramRed = SmoothHistogram(fileItem.FileInfo.HistogramRed);
//fileItem.FileInfo.HistogramLuminance = SmoothHistogram(fileItem.FileInfo.HistogramLuminance);
}
示例5: AdvGamePage
public AdvGamePage()
{
InitializeComponent();
var fileStream1 = Application.GetResourceStream(new Uri("TestImages/TestingUpper.png", UriKind.Relative));
var s1 = new BitmapImage();
using (var stream = fileStream1.Stream)
{
s1.SetSource(stream);
}
var upper = new WriteableBitmap(s1);
var fileStream2 = Application.GetResourceStream(new Uri("TestImages/TestingLower.png", UriKind.Relative));
var s2 = new BitmapImage();
using (var stream = fileStream2.Stream)
{
s2.SetSource(stream);
}
var lower = new WriteableBitmap(s2);
var upperEx = upper.GetBitmapContext();
var lowerEx = lower.GetBitmapContext();
UpperImage.Source = upper;
LowerImage.Source = lower;
upper.ForEach((x, y, color) =>
{
var lowerColor = lower.GetPixel(x, y);
return new Color
{
R = (byte) (color.R - lowerColor.R),
G = (byte) (color.G - lowerColor.G),
B = (byte) (color.B - lowerColor.B),
A = color.A
};
});
upper.Invalidate();
var changesPixels = new List<Point>();
upper.ForEach((x, y, color) =>
{
if (upper.GetPixel(x, y).R != 0)
{
lower.FillEllipseCentered(x, y, 10, 10, Colors.Red);
changesPixels.Add(new Point(x, y));
}
return color;
});
lower.Invalidate();
//var indicator = new bool[480, 320];
var startingPoint = changesPixels[0];
var newPointToBeInvestigate = new List<Point> { startingPoint };
changesPixels.Remove(startingPoint);
//indicator[(int)startingPoint.X, (int)startingPoint.Y] = true;
System.Diagnostics.Debug.WriteLine(DateTime.Now);
var i = 0;
while (newPointToBeInvestigate.Count > 0 && i < newPointToBeInvestigate.Count)
{
var newPixels = changesPixels.Where(p => DistanceBetweenPoints(newPointToBeInvestigate[i], p)).ToList();
foreach (var np in newPixels)
{
changesPixels.Remove(np);
newPointToBeInvestigate.Add(np);
}
//foreach (var newP in newPixels.Where(newP => !indicator[(int)newP.X, (int)newP.Y]))
//{
// indicator[(int)newP.X, (int)newP.Y] = true;
//}
i++;
}
System.Diagnostics.Debug.WriteLine(DateTime.Now);
foreach (var changeP in newPointToBeInvestigate)
{
lower.SetPixel((int)changeP.X, (int)changeP.Y, Colors.Green);
}
lower.Invalidate();
}
示例6: FillRectangle2
public static void FillRectangle2(WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
{
using (var context = bmp.GetBitmapContext())
{
unsafe
{
// Use refs for faster access (really important!) speeds up a lot!
int w = bmp.PixelWidth;
int h = bmp.PixelHeight;
var pixels = context.Pixels;
// Check boundaries
if (x1 < 0)
{
x1 = 0;
}
if (y1 < 0)
{
y1 = 0;
}
if (x2 < 0)
{
x2 = 0;
}
if (y2 < 0)
{
y2 = 0;
}
if (x1 >= w)
{
x1 = w - 1;
}
if (y1 >= h)
{
y1 = h - 1;
}
if (x2 >= w)
{
x2 = w - 1;
}
if (y2 >= h)
{
y2 = h - 1;
}
unchecked
{
for (int y = y1; y <= y2; y++)
{
for (int i = y*w + x1; i <= y*w + x2; i++)
{
byte oneOverAlpha = (byte) (255 - color.A);
int c = pixels[i];
int r = ((byte) (c >> 16)*oneOverAlpha ) >> 8;
int g = ((byte) (c >> 8)*oneOverAlpha ) >> 8;
int b = ((byte) (c >> 0)*oneOverAlpha ) >> 8;
pixels[i] = 255 << 24 | r << 16 | g << 8 | b;
}
}
}
}
}
}
示例7: GenerateOctreeForBitmap
private void GenerateOctreeForBitmap(WriteableBitmap wbm)
{
unsafe
{
using (var context = wbm.GetBitmapContext())
{
for (int i = 0; i < context.Width; i++)
{
for (int j = 0; j < context.Height; j++)
{
var c = context.Pixels[j * context.Width + i];
var a = (byte)(c >> 24);
int ai = a;
if (ai == 0)
{
ai = 1;
}
ai = ((255 << 8) / ai);
var color = Color.FromArgb(a,
(byte)((((c >> 16) & 0xFF) * ai) >> 8),
(byte)((((c >> 8) & 0xFF) * ai) >> 8),
(byte)((((c & 0xFF) * ai) >> 8)));
Insert(color);
}
}
}
}
}
示例8: Draw
public void Draw(WriteableBitmap writeableBmp)
{
if (writeableBmp != null)
{
// Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block
using (writeableBmp.GetBitmapContext())
{
writeableBmp.Clear();
Draw(writeableBmp, this.Root);
#if SILVERLIGHT
writeableBmp.Invalidate();
#endif
}
}
}
示例9: AddToBitmap
/// <summary>
/// Draws the body joints and bones to a WriteableBitmap
/// </summary>
/// <param name="bodies"></param>
/// <param name="bitmap">the bitmap to which to draw the bodies</param>
/// <param name="boneColor">color to use for drawing bones</param>
/// <param name="jointColor">color to use for drawing joints</param>
public static void AddToBitmap(this IEnumerable<IBody> bodies, WriteableBitmap bitmap, Color boneColor, Color jointColor)
{
using (var context = bitmap.GetBitmapContext())
foreach (var body in bodies)
if (body.IsTracked)
AddBodyToContext(body, context, boneColor, jointColor);
}