本文整理汇总了C#中System.Windows.Media.Imaging.WriteableBitmap.CopyPixels方法的典型用法代码示例。如果您正苦于以下问题:C# WriteableBitmap.CopyPixels方法的具体用法?C# WriteableBitmap.CopyPixels怎么用?C# WriteableBitmap.CopyPixels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.WriteableBitmap
的用法示例。
在下文中一共展示了WriteableBitmap.CopyPixels方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DrawRectangle
/// <summary>
/// Draws a red rectangle on the bitmap
/// </summary>
/// <param name="bitmap">The bitmap to draw on</param>
/// <param name="rect">The position of the rectangle to draw</param>
public static void DrawRectangle( WriteableBitmap bitmap, Rect rect )
{
int stride = ( bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7 ) / 8;
byte[] pixelByteArray = new byte[bitmap.PixelHeight * stride];
bitmap.CopyPixels( pixelByteArray, stride, 0 );
for ( int column = 0; column < bitmap.PixelWidth; column++ )
{
for ( int row = 0; row < bitmap.PixelHeight; row++ )
{
if ( ( column == rect.X && row >= rect.Y && row <= rect.Y + rect.Height ) ||
( row == rect.Y && column >= rect.X && column <= rect.X + rect.Width ) ||
( column == rect.X + rect.Width && row >= rect.Y && row <= rect.Y + rect.Height ) ||
( row == rect.Y + rect.Height && column >= rect.X && column <= rect.X + rect.Width ) )
{
int index = row * stride + 4 * column;
pixelByteArray[index] = 0;
pixelByteArray[index + 1] = 0;
pixelByteArray[index + 2] = 255;
}
}
}
bitmap.WritePixels( new Int32Rect( 0, 0, bitmap.PixelWidth, bitmap.PixelHeight ), pixelByteArray, stride, 0 );
}
示例2: WriteableBitmapWrapper
public WriteableBitmapWrapper(WriteableBitmap source)
{
_inner = source;
_width = source.PixelWidth;
int width = _inner.PixelWidth;
int height = _inner.PixelHeight;
_pixels = new int[width * height];
_inner.CopyPixels(_pixels, width * 4, 0);
}
示例3: BitmapSourceToMat
public static Mat<byte> BitmapSourceToMat(BitmapSource source)
{
WriteableBitmap writeableBmp = new WriteableBitmap(source);
byte[] rawData = new byte[writeableBmp.PixelWidth * writeableBmp.PixelHeight];
writeableBmp.CopyPixels(rawData, writeableBmp.PixelWidth, 0);
Mat<byte> image = new Mat<byte>(writeableBmp.PixelHeight, writeableBmp.PixelWidth);
for (int i = 0; i < writeableBmp.PixelHeight; i++)
for (int j = 0; j < writeableBmp.PixelWidth; j++)
image[i, j] = rawData[i * writeableBmp.PixelWidth + j];
return image;
}
示例4: Image_ImageOpened
/// <summary>
/// Obtains the image data once it is loaded
/// </summary>
protected void Image_ImageOpened(object sender, RoutedEventArgs e)
{
if (bitmap == null)
{
bitmap = new WriteableBitmap((BitmapImage)image.Source);
image.Source = bitmap;
#if WPF
int w = bitmap.PixelWidth;
int h = bitmap.PixelHeight;
pixelData = new int[w * h];
int widthInBytes = 4 * w;
bitmap.CopyPixels(pixelData, widthInBytes, 0);
#else
pixelData = bitmap.Pixels;
#endif
}
}
示例5: TileSet
public TileSet(System.Drawing.Bitmap source, int tileWidth, int tileHeight)
{
_source = new WriteableBitmap(Imaging.CreateBitmapSourceFromHBitmap(
source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(source.Width, source.Height)));
TileWidth = tileWidth;
TileHeight = tileHeight;
_tilesPerRow = (int)_source.Width / TileWidth;
_bytesPerPixel = _source.Format.BitsPerPixel / BITS_PER_BYTE;
_stride = _source.PixelWidth * _bytesPerPixel;
_originalPixels = new byte[_stride * _source.PixelHeight];
_source.CopyPixels(_originalPixels, _stride, 0);
_tiles = new List<CroppedBitmap>();
GenerateTiles();
}
示例6: execute
public void execute()
{
if (Path != null)
origin = LoadBitmap(Path);
try
{
if (origin != null)
{
origin = origin.Resize(68, 42, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
origin.CopyPixels(new Int32Rect(0,0,origin.PixelWidth,origin.PixelHeight), monitor.BackBuffer,
monitor.BackBufferStride * monitor.PixelHeight, monitor.BackBufferStride);
}
}
catch (AccessViolationException e)
{
MessageBox.Show(e.Message);
}
}
示例7: ColorBitmap
/// <summary>
/// Colors the connected objects in the bitmap. Note this function assumes the image is thresholded
/// </summary>
/// <param name="bitmap">The bitmap to color</param>
/// <returns>A list of final colors in the image, one for each object</returns>
public static List<Color> ColorBitmap( WriteableBitmap bitmap )
{
int stride = ( bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7 ) / 8;
byte[] pixelArray = new byte[bitmap.PixelHeight * stride];
bitmap.CopyPixels( pixelArray, stride, 0 );
var colorMergeDictionary = new Dictionary<int, int>();
var colors = new List<Color>();
var random = new Random();
for ( int i = 0; i < bitmap.PixelHeight; i++ )
{
for ( int j = 0; j < bitmap.PixelWidth; j++ )
{
int index = i * stride + 4 * j;
// Check if it is a foreground object and has not been colored
if ( GetPixelColor( pixelArray, index ) == Colors.Black )
{
var color = GetPixelColoring( pixelArray, index, stride, colors, random, colorMergeDictionary );
pixelArray[index] = color.B;
pixelArray[index + 1] = color.G;
pixelArray[index + 2] = color.R;
// Keep track of all the colors in the image so
// we can merge them later
if ( !colors.Contains( color ) )
{
colors.Add( color );
colorMergeDictionary[colors.Count - 1] = colors.Count - 1;
}
}
}
}
var trueColors = MergeConnectedColors( pixelArray, colors, colorMergeDictionary );
bitmap.WritePixels( new Int32Rect( 0, 0, bitmap.PixelWidth, bitmap.PixelHeight ), pixelArray, stride, 0 );
return trueColors;
}
示例8: Draw
public unsafe override void Draw(WriteableBitmap desktop)
{
// Avoid exception if window is dragged bottom of screen
if (rectangle.Top + rectangle.Height >= framebuffer.Height)
{
rectangle.Height = framebuffer.Height - rectangle.Top - 1;
}
if ((rectangle.Y * desktop.PixelWidth + rectangle.X) < (source.Y * desktop.PixelWidth + source.X))
{
Int32Rect srcRect = new Int32Rect(source.X, source.Y, rectangle.Width, rectangle.Height);
desktop.WritePixels(srcRect, desktop.BackBuffer, desktop.BackBufferStride * desktop.PixelHeight, desktop.PixelWidth * 4, rectangle.X, rectangle.Y);
}
else
{
Int32[] pixelBuf = new Int32[rectangle.Width * rectangle.Height];
desktop.CopyPixels(new Int32Rect(source.X, source.Y, rectangle.Width, rectangle.Height), pixelBuf, rectangle.Width * 4, 0);
desktop.WritePixels(new Int32Rect(0, 0, rectangle.Width, rectangle.Height), pixelBuf, rectangle.Width * 4, rectangle.X, rectangle.Y);
}
}
示例9: ApplyBlobMaskToOtherBitmaps
public static void ApplyBlobMaskToOtherBitmaps(WriteableBitmap bitmap, WriteableBitmap gradientBitmapRef,
WriteableBitmap realBitmapRef, System.Windows.Media.Color blobColor)
{
int stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8;
byte[] pixelByteArray = new byte[bitmap.PixelHeight * stride];
byte[] gradientByteArray = new byte[gradientBitmapRef.PixelHeight * stride];
byte[] realByteArray = new byte[realBitmapRef.PixelHeight * stride];
bitmap.CopyPixels(pixelByteArray, stride, 0);
gradientBitmapRef.CopyPixels(gradientByteArray, stride, 0);
realBitmapRef.CopyPixels(realByteArray, stride, 0);
for (int column = 0; column < bitmap.PixelWidth; column++)
{
for (int row = 0; row < bitmap.PixelHeight; row++)
{
int index = row * stride + 4 * column;
var pixelColor = new System.Windows.Media.Color();
pixelColor = System.Windows.Media.Color.FromRgb(pixelByteArray[index + 2],
pixelByteArray[index + 1], pixelByteArray[index]);
if (pixelColor != blobColor)
{
gradientByteArray[index] = 0;
gradientByteArray[index + 1] = 0;
gradientByteArray[index + 2] = 0;
realByteArray[index] = 0;
realByteArray[index + 1] = 0;
realByteArray[index + 2] = 0;
}
}
}
gradientBitmapRef.WritePixels(new Int32Rect(0, 0, gradientBitmapRef.PixelWidth, gradientBitmapRef.PixelHeight), gradientByteArray, stride, 0);
realBitmapRef.WritePixels(new Int32Rect(0, 0, realBitmapRef.PixelWidth, realBitmapRef.PixelHeight), realByteArray, stride, 0);
}
示例10: Can_Create_Color_Image
public void Can_Create_Color_Image()
{
var imageFactory = new RgbImageSourceFactory();
var pointerFactory = new ArrayToPointerFactory();
var data = new ushort[20 * 10 * 2];
for (int index = data.Length / 2; index < data.Length; index++)
{
data[index] = ushort.MaxValue;
}
var bitmap = new WriteableBitmap(20, 10, 96, 96, PixelFormats.Bgr24, null);
var pointer = pointerFactory.CreatePointer(data);
try
{
imageFactory.CreateImage(bitmap, pointer);
}
finally
{
pointerFactory.Destroy(pointer);
}
int height = bitmap.PixelHeight;
int width = bitmap.PixelWidth;
int stride = bitmap.PixelWidth * 3;
byte[] pixelByteArray = new byte[bitmap.PixelHeight * stride];
bitmap.CopyPixels(pixelByteArray, stride, 0);
Assert.AreEqual(0, pixelByteArray[0]);
Assert.AreEqual(0, pixelByteArray[1]);
Assert.AreEqual(0, pixelByteArray[2]);
Assert.AreEqual(255, pixelByteArray[10 * 3 + 9 * stride]);
Assert.AreEqual(255, pixelByteArray[10 * 3 + 9 * stride + 1]);
Assert.AreEqual(255, pixelByteArray[10 * 3 + 9 * stride + 2]);
}
示例11: Create
public Image Create(BitmapSource bitmapSource)
{
if (bitmapSource == null)
return Image.Empty;
try
{
var width = bitmapSource.PixelWidth;
var height = bitmapSource.PixelHeight;
var format = bitmapSource.Format;
var bitmap = new WriteableBitmap(bitmapSource);
var bytesPerPixel = (bitmap.Format.BitsPerPixel + 7)/8;
var stride = 4*((bitmapSource.PixelWidth*bytesPerPixel + 3)/4);
var length = stride*bitmapSource.PixelHeight;
var pixels = new byte[length];
bitmap.CopyPixels(pixels, stride, 0);
return new Image(format.BitsPerPixel, width, height, pixels, format.ToString());
}
catch (Exception)
{
return Image.Empty;
}
}
示例12: ColorizeBitmap
public static BitmapSource ColorizeBitmap(BitmapSource bmp, Color tint)
{
var wbmp = new WriteableBitmap(bmp);
var arr = new uint[wbmp.PixelWidth * wbmp.PixelHeight];
wbmp.CopyPixels(arr, wbmp.PixelWidth * 4, 0);
for (int i = 0; i < arr.Length; ++i)
{
byte a = (byte)((arr[i] >> 24) & 0xff);
byte r = (byte)((arr[i] >> 16) & 0xff);
byte g = (byte)((arr[i] >> 8) & 0xff);
byte b = (byte)((arr[i] >> 0) & 0xff);
var c = Color.FromArgb(a, r, g, b);
c = TileSetHelpers.TintColor(c, tint);
arr[i] = (uint)((c.A << 24) | (c.R << 16) | (c.G << 8) | (c.B << 0));
}
wbmp.WritePixels(new Int32Rect(0, 0, wbmp.PixelWidth, wbmp.PixelHeight), arr, wbmp.PixelWidth * 4, 0);
return wbmp;
}
示例13: ModifyImage
// Sample --
private void ModifyImage(string method)
{
WriteableBitmap modifiedImage;
try
{
BitmapSource bitmapSource = new FormatConvertedBitmap(originalImage,
PixelFormats.Bgra32,
null, 0);
modifiedImage = new WriteableBitmap(bitmapSource);
}
catch (ArgumentNullException)
{
return;
}
int height = modifiedImage.PixelHeight;
int width = modifiedImage.PixelWidth;
int[] pixelData = new int[width*height];
int widthInByte = 4*width;
modifiedImage.CopyPixels(pixelData, widthInByte, 0);
// ----------- Extern call here --
int[,] pixels;
switch (method)
{
case "D":
double gmin = 0;
Double.TryParse(GMin.Text, out gmin);
double gmax = 255;
Double.TryParse(GMax.Text, out gmax);
imageProcessing.TresholdD(ref pixelData, (int)gmin, (int)gmax);
break;
case "E":
double fmin = 0;
double fmax = 255;
Double.TryParse(FMin.Text, out fmin);
Double.TryParse(FMax.Text, out fmax);
imageProcessing.TresholdE(ref pixelData, (int)fmin, (int)fmax);
break;
case "MIN":
pixels = ArrayConverter.To2D(pixelData, modifiedImage.PixelWidth, modifiedImage.PixelHeight);
pixels = imageProcessing.MinFilter(pixels);
pixelData = ArrayConverter.ToLinear(pixels);
break;
case "MAX":
pixels = ArrayConverter.To2D(pixelData, modifiedImage.PixelWidth, modifiedImage.PixelHeight);
pixels = imageProcessing.MaxFilter(pixels);
pixelData = ArrayConverter.ToLinear(pixels);
break;
case "MINMAX":
pixels = ArrayConverter.To2D(pixelData, modifiedImage.PixelWidth, modifiedImage.PixelHeight);
pixels = imageProcessing.MinMaxFilter(pixels);
pixelData = ArrayConverter.ToLinear(pixels);
break;
case "BLUR":
pixels = ArrayConverter.To2D(pixelData, modifiedImage.PixelWidth, modifiedImage.PixelHeight);
pixels = imageProcessing.Blur(pixels);
pixelData = ArrayConverter.ToLinear(pixels);
break;
default:
break;
}
// --------------------------------
modifiedImage.WritePixels(new Int32Rect(0, 0, width, height), pixelData, widthInByte, 0);
ModifiedImage.Source = modifiedImage;
DrawHistogram(pixelData, "MODIFIED");
}
示例14: GenerateMask
/// <summary>
/// Méthode qui permet de générer la carte de chaleur ou de transparence de l'image
/// </summary>
/// <param name="mode">Mode sélectionné</param>
/// <param name="img">Image sur laquelle on appose le masque</param>
private void GenerateMask(string mode, ImageExp img)
{
ImageBmp = new WriteableBitmap(new BitmapImage(new Uri(img.Acces, UriKind.RelativeOrAbsolute)));
imgBackground.Source = ImageBmp;
// Taille de l'image d'origine
//int width = imageBitmap.PixelWidth;
//int height = imageBitmap.PixelHeight;
int width = 1680;
int height = 900;
// Creation du masque a partir du bitmap d'origine
WriteableBitmap bitmap = new WriteableBitmap(ImageBmp);
// Pixels Destination (r,g,b,a)
byte[, ,] pixels = new byte[height, width, 4];
// Pixels Source
int strideSource = ImageBmp.PixelWidth * 4;
int sizeSource = ImageBmp.PixelHeight * strideSource;
byte[] pixelSource = new byte[sizeSource];
bitmap.CopyPixels(pixelSource, strideSource, 0);
double max = Data[AppData.ImagesExp[img.Numero-1]].Cast<double>().Max();
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
// le numéro de l'image à indiquer est un en dessous
double tps = Data[AppData.ImagesExp[img.Numero-1]][row, col];
// Pixel d'origine
int indexSource = row * strideSource + 4 * col;
double coef = tps / max;
// 0 : bleu, 1 : vert, 2 : rouge
// Cas où on demande le gris
if (mode == "gris")
{
pixels[row, col, 0] = (byte)(pixelSource[indexSource] * coef);
pixels[row, col, 1] = (byte)(pixelSource[indexSource + 1] * coef);
pixels[row, col, 2] = (byte)((pixelSource[indexSource + 2]) * coef);
}
// Cas où l'utilisateur demande de la couleur
else
{
int x = (int)Math.Floor((1000 / max) * (max-tps));
if (x >= 0 && x < 255)
{
pixels[row, col, 2] = (byte)(pixelSource[indexSource+2]/2 + (255)/2);
pixels[row, col, 1] = (byte)(pixelSource[indexSource+1]/2 + (x)/2);
pixels[row, col, 0] = (byte)( pixelSource[indexSource]/2 + (0)/2);
}
if (x >= 255 && x < 510)
{
pixels[row, col, 2] = (byte)(pixelSource[indexSource + 2] / 2 + (510 - x) / 2);
pixels[row, col, 1] = (byte)(pixelSource[indexSource + 1] / 2 + (255) / 2);
pixels[row, col, 0] = (byte)(pixelSource[indexSource] / 2 + (0) / 2);
}
if (x >= 510 && x < 765)
{
pixels[row, col, 2] = (byte)(pixelSource[indexSource + 2] / 2 + (0) / 2);
pixels[row, col, 1] = (byte)(pixelSource[indexSource + 1] / 2 + (255) / 2);
pixels[row, col, 0] = (byte)(pixelSource[indexSource] / 2 + (x-510) / 2);
}
if (x >= 765 && x < 1020)
{
pixels[row, col, 2] = (byte)(pixelSource[indexSource + 2] / 2 + (0) / 2);
pixels[row, col, 1] = (byte)(pixelSource[indexSource + 1] / 2 + (1020-x) / 2);
pixels[row, col, 0] = (byte)(pixelSource[indexSource] / 2 + (255) / 2);
}
if (x >= 1020 && x < 1275)
{
pixels[row, col, 2] = (byte)(pixelSource[indexSource + 2] / 2 + (x-1020) / 2);
pixels[row, col, 1] = (byte)(pixelSource[indexSource + 1] / 2 + (0) / 2);
pixels[row, col, 0] = (byte)(pixelSource[indexSource] / 2 + (255) / 2);
}
if (x >= 1275 && x <= 1530)
{
pixels[row, col, 2] = (byte)(pixelSource[indexSource + 2] / 2 + (255) / 2);
pixels[row, col, 1] = (byte)(pixelSource[indexSource + 1] / 2 + (0) / 2);
pixels[row, col, 0] = (byte)(pixelSource[indexSource] / 2 + (1530-x) / 2);
}
}
// Alpha
pixels[row, col, 3] = 255;
}
}
//.........这里部分代码省略.........
示例15: PixelColorOfCentralBlob
public static System.Windows.Media.Color PixelColorOfCentralBlob(WriteableBitmap bitmap)
{
int stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8;
ConcurrentDictionary<System.Windows.Media.Color, int > colorDict =
new ConcurrentDictionary<System.Windows.Media.Color, int>();
byte[] pixelByteArray = new byte[bitmap.PixelHeight * stride];
bitmap.CopyPixels(pixelByteArray, stride, 0);
//Draw a Horizontal Line accross the middle
for (int column = 0; column < bitmap.PixelWidth; column++)
{
var color = new System.Windows.Media.Color();
int row = (int)(bitmap.PixelHeight/2.0);
int index = row * stride + 4 * column;
int R = Convert.ToInt32(pixelByteArray[index+2]);
int G = Convert.ToInt32(pixelByteArray[index+1]);
int B = Convert.ToInt32(pixelByteArray[index]);
color = System.Windows.Media.Color.FromRgb(pixelByteArray[index + 2],
pixelByteArray[index + 1], pixelByteArray[index]);
if (!(R == 255 && B == 255 && G == 255) && !(R== 0 && B == 0 && G == 0))
{
colorDict.AddOrUpdate(color, 1, (id, count) => count + 1);
}
}
int maxValue = 0;
var colorKey = new System.Windows.Media.Color();
foreach (var item in colorDict)
{
if (item.Value > maxValue)
{
colorKey = item.Key;
maxValue = item.Value;
}
}
return colorKey;
}