本文整理汇总了C#中System.Windows.Media.Imaging.CroppedBitmap.CopyPixels方法的典型用法代码示例。如果您正苦于以下问题:C# CroppedBitmap.CopyPixels方法的具体用法?C# CroppedBitmap.CopyPixels怎么用?C# CroppedBitmap.CopyPixels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.CroppedBitmap
的用法示例。
在下文中一共展示了CroppedBitmap.CopyPixels方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetColorFromImage
private Color GetColorFromImage()
{
var color = Colors.Transparent;
try
{
BitmapSource bitmapSource = ImageColors.Source as BitmapSource;
if (bitmapSource != null)
{
double x = Mouse.GetPosition(ImageColors).X;
x *= bitmapSource.PixelWidth / ImageColors.ActualWidth;
if ((int)x > bitmapSource.PixelWidth - 1)
x = bitmapSource.PixelWidth - 1;
else if (x < 0) x = 0;
double y = Mouse.GetPosition(ImageColors).Y;
y *= bitmapSource.PixelHeight / ImageColors.ActualHeight;
if ((int)y > bitmapSource.PixelHeight - 1)
y = bitmapSource.PixelHeight - 1;
else if (y < 0) y = 0;
CroppedBitmap cb = new CroppedBitmap(bitmapSource, new Int32Rect((int)x, (int)y, 1, 1));
byte[] pixels = new byte[4];
cb.CopyPixels(pixels, 4, 0);
if (pixels[3] == byte.MaxValue)
{
color = Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]);
}
}
}
catch (Exception ex)
{
Log.Add(ex);
}
return color;
}
示例2: Image_MouseMove
private void Image_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
var AssociatedObject = this;
// Retrieve the coordinate of the mouse position in relation to the supplied image.
Point point = e.GetPosition(AssociatedObject);
// Use RenderTargetBitmap to get the visual, in case the image has been transformed.
var renderTargetBitmap = new RenderTargetBitmap((int) AssociatedObject.ActualWidth,
(int) AssociatedObject.ActualHeight,
96, 96, PixelFormats.Default);
renderTargetBitmap.Render(AssociatedObject);
// Make sure that the point is within the dimensions of the image.
if ((point.X <= renderTargetBitmap.PixelWidth) && (point.Y <= renderTargetBitmap.PixelHeight))
{
// Create a cropped image at the supplied point coordinates.
var croppedBitmap = new CroppedBitmap(renderTargetBitmap,
new Int32Rect((int) point.X, (int) point.Y, 1, 1));
// Copy the sampled pixel to a byte array.
var pixels = new byte[4];
croppedBitmap.CopyPixels(pixels, 4, 0);
// Assign the sampled color to a SolidColorBrush and return as conversion.
var SelectedColor = Color.FromArgb(255, pixels[2], pixels[1], pixels[0]);
TextBox.Text = "#" + SelectedColor.ToString().Substring(3);
Label.Background = new SolidColorBrush(SelectedColor);
}
}
示例3: GetColorFromImage
private Color GetColorFromImage(Point p)
{
try
{
var bounds = VisualTreeHelper.GetDescendantBounds(this);
var rtb = new RenderTargetBitmap((int) bounds.Width, (int) bounds.Height, 96, 96, PixelFormats.Default);
rtb.Render(this);
byte[] arr;
var png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (var stream = new MemoryStream())
{
png.Save(stream);
arr = stream.ToArray();
}
BitmapSource bitmap = BitmapFrame.Create(new MemoryStream(arr));
var pixels = new byte[4];
var cb = new CroppedBitmap(bitmap, new Int32Rect((int) p.X, (int) p.Y, 1, 1));
cb.CopyPixels(pixels, 4, 0);
return Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]);
}
catch (Exception)
{
return ColorBox.Color;
}
}
示例4: Image_MouseDown
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
try
{
var cb = new CroppedBitmap((BitmapSource) (((Image) e.Source).Source),
new Int32Rect((int) Mouse.GetPosition(e.Source as Image).X,
(int) Mouse.GetPosition(e.Source as Image).Y, 1, 1));
_pixels = new byte[4];
try
{
cb.CopyPixels(_pixels, 4, 0);
SetColor(Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]));
UpdateMarkerPosition();
if (OnColorSelected != null)
OnColorSelected(SelectedColor);
}
catch
{
// not logged
}
UpdateSlider();
}
catch (Exception)
{
// not logged
}
}
示例5: wheel_MouseMove
void wheel_MouseMove(object sender, MouseEventArgs e)
{
try
{
CroppedBitmap cb = new CroppedBitmap(wheel.Source as BitmapSource, new Int32Rect((int)Mouse.GetPosition(this).X, (int)Mouse.GetPosition(this).Y, 1, 1));
pixels = new byte[4];
try
{
cb.CopyPixels(pixels, 4, 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Console.WriteLine(pixels[0] + ":" + pixels[1] + ":" + pixels[2] + ":" + pixels[3]);
rec.Fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(pixels[2], pixels[1], pixels[0]));
}
catch (Exception exc)
{
}
}
示例6: Image_MouseDown
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
try
{
var cb = new CroppedBitmap((BitmapSource)(((Image)e.Source).Source), new Int32Rect((int)Mouse.GetPosition(e.Source as Image).X, (int)Mouse.GetPosition(e.Source as Image).Y, 1, 1));
_pixels = new byte[4];
try
{
cb.CopyPixels(_pixels, 4, 0);
UpdateCurrentColor();
UpdateMarkerPosition();
}
catch
{
}
UpdateSlider();
}
catch (Exception)
{
}
}
示例7: UpdateCursorEllipse
/// <summary>
/// Update the mouse cursor ellipse position.
/// </summary>
private void UpdateCursorEllipse(Color searchColor)
{
// Scan the canvas image for a color which matches the search color
CroppedBitmap cb;
Color tempColor = new Color();
byte[] pixels = new byte[4];
int searchY = 0;
int searchX = 0;
for (searchY = 0; searchY <= this.canvasImage.Width - 1; searchY++)
{
for (searchX = 0; searchX <= this.canvasImage.Height - 1; searchX++)
{
cb = new CroppedBitmap(this.ColorImage.Source as BitmapSource, new Int32Rect(searchX, searchY, 1, 1));
cb.CopyPixels(pixels, 4, 0);
tempColor = new Color(pixels[2], pixels[1], pixels[0]);
if (tempColor == searchColor) break;
}
if (tempColor == searchColor) break;
}
// Default to the top left if no match is found
if (tempColor != searchColor)
{
searchX = 0;
searchY = 0;
}
// Update the mouse cursor ellipse position
this.ellipsePixel.SetValue(Canvas.LeftProperty, ((double)searchX - (this.ellipsePixel.Width / 2.0)));
this.ellipsePixel.SetValue(Canvas.TopProperty, ((double)searchY - (this.ellipsePixel.Width / 2.0)));
}
示例8: GetColorFromColorRectangle
/// <summary>
/// Retrieves the color from the ColorRectangle at the point where the mouse is in the rectangle
/// </summary>
/// <returns>Returns the color in an Argb format</returns>
private Color GetColorFromColorRectangle()
{
PresentationSource source = PresentationSource.FromVisual(this);
//Gets the mouses current position relative to the color rectangle
Point point = Mouse.GetPosition(ColorRectangle);
//Creates a bitmap of the color rectangle and renders it
RenderTargetBitmap bmp = new RenderTargetBitmap((int)ColorRectangle.Width, (int)ColorRectangle.Height, 96 * source.CompositionTarget.TransformToDevice.M11, 96 * source.CompositionTarget.TransformToDevice.M22, PixelFormats.Default);
bmp.Render(ColorRectangle);
//If the mouse position is within the color rectangle
if ((point.X < bmp.PixelWidth) && (point.Y < bmp.PixelHeight))
{
//Create a cropped bitmap with the dimensions of height, width = 1, at the mouses position
CroppedBitmap croppedBitmap = new CroppedBitmap(bmp, new Int32Rect((int)point.X, (int)point.Y, 1, 1));
//Copies the pixel stored in the cropped bitmap to a byte array
byte[] pixels = new byte[4];
croppedBitmap.CopyPixels(pixels, 4, 0);
//Returns the color generated from the byte array to the calling function
return Color.FromArgb(255, pixels[2], pixels[1], pixels[0]);
}
//IF the mouse is out of bounds of the ColorRectangle, returns a default color
else
return Color.FromArgb(0, 0, 0, 0);
}
示例9: Render
internal Present Render(MapRectangle mapRect, System.Drawing.Size size, bool useDocumentTransparency, bool exactColors)
{
Monitor.Enter(this);
Present result;
try
{
RectangleD rectangleD = new RectangleD(mapRect.lon0 * (double)this.boundingBox.Width - 0.5, -mapRect.lat1 * (double)this.boundingBox.Height + (double)this.actualBoundingBox.Height - 0.5, (mapRect.lon1 - mapRect.lon0) * (double)this.boundingBox.Width + (double)this.hackRectangleAdjust, (mapRect.lat1 - mapRect.lat0) * (double)this.boundingBox.Height + (double)this.hackRectangleAdjust);
RectangleD rectangleD2 = rectangleD.Grow(2.0);
RectangleD r = new RectangleD((double)this.actualBoundingBox.X, (double)this.actualBoundingBox.Y, (double)this.actualBoundingBox.Width, (double)this.actualBoundingBox.Height);
RectangleD dest = new RectangleD(0.0, 0.0, (double)size.Width, (double)size.Height);
ScaleAndTranslate scaleAndTranslate = new ScaleAndTranslate(rectangleD, dest);
RectangleD rectangleD3 = rectangleD2.Intersect(r).Round();
RectangleD rectangleD4 = scaleAndTranslate.Apply(rectangleD.Intersect(r));
RectangleD rectangleD5 = scaleAndTranslate.Apply(rectangleD3);
ScaleAndTranslate scaleAndTranslate2 = new ScaleAndTranslate(-rectangleD5.X, -rectangleD5.Y);
RectangleD rectangleD6 = scaleAndTranslate2.Apply(rectangleD4);
GDIBigLockedImage gDIBigLockedImage = new GDIBigLockedImage(size, "WPFVerb");
GDIBigLockedImage image = gDIBigLockedImage;
if (!rectangleD3.IntIsEmpty() && !rectangleD6.IntIsEmpty())
{
try
{
GDIBigLockedImage obj;
Monitor.Enter(obj = gDIBigLockedImage);
try
{
Bitmap bitmap = (Bitmap)gDIBigLockedImage.IPromiseIAmHoldingGDISLockSoPleaseGiveMeTheImage();
BitmapSource source = new CroppedBitmap(this.primarySource, rectangleD3.ToInt32Rect());
BitmapSource source2 = new TransformedBitmap(source, scaleAndTranslate.ToScaleTransform());
BitmapSource bitmapSource = new CroppedBitmap(source2, rectangleD6.ToInt32Rect());
Int32Rect int32Rect = rectangleD4.ToInt32Rect();
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
try
{
IntPtr buffer = new IntPtr(bitmapData.Scan0.ToInt32() + int32Rect.Y * bitmapData.Stride + int32Rect.X * 4);
bitmapSource.CopyPixels(new Int32Rect(0, 0, bitmapSource.PixelWidth, bitmapSource.PixelHeight), buffer, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
}
finally
{
Monitor.Exit(obj);
}
}
catch (Exception ex)
{
if (BigDebugKnob.theKnob.debugFeaturesEnabled)
{
this.LabelThisImage(gDIBigLockedImage, ex.ToString());
}
}
if (useDocumentTransparency)
{
image = gDIBigLockedImage;
}
else
{
GDIBigLockedImage gDIBigLockedImage2 = new GDIBigLockedImage(size, "WPFVerb-untransparent");
GDIBigLockedImage obj2;
Monitor.Enter(obj2 = gDIBigLockedImage2);
try
{
Graphics graphics = gDIBigLockedImage2.IPromiseIAmHoldingGDISLockSoPleaseGiveMeTheGraphics();
graphics.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, size.Width, size.Height);
graphics.DrawImage(gDIBigLockedImage.IPromiseIAmHoldingGDISLockSoPleaseGiveMeTheImage(), 0, 0);
graphics.Dispose();
image = gDIBigLockedImage2;
gDIBigLockedImage.Dispose();
}
finally
{
Monitor.Exit(obj2);
}
}
}
result = new ImageRef(new ImageRefCounted(image));
}
finally
{
Monitor.Exit(this);
}
return result;
}
示例10: IsEffectiveHitOnPngImage
/// <summary>
/// 获取某点鼠标点击颜色值
/// </summary>
/// <param name="bitmapsource">当前点击图片</param>
/// <param name="x">鼠标点击坐标</param>
/// <param name="y">鼠标点击坐标</param>
/// <returns>是否有效点击</returns>
public static bool IsEffectiveHitOnPngImage(BitmapSource bitmapsource, double x, double y)
{
bool isEffective = false;
CroppedBitmap crop = new CroppedBitmap(bitmapsource as BitmapSource, new Int32Rect((int)x, (int)y, 1, 1));
byte[] pixels = new byte[4];
try
{
crop.CopyPixels(pixels, 4, 0);
crop = null;
}
catch (Exception e)
{
//MessageBox.Show(ee.ToString());
}
Color tempColor = Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]);
if (tempColor.ToString() == "#00000000")
{
isEffective = false;//点击到非有效部分
}
else
{
isEffective = true;//点击到有效部分
}
return isEffective;
}
示例11: calcMacbethStats
public int calcMacbethStats(byte[] pixels, int width, int height)
{
// //create array of rectangles
System.Windows.Int32Rect[] macbethRects = new System.Windows.Int32Rect[24];
for (int i = 0; i < 24; i++) {
macbethRects[i].Width = (int)(width * 0.06);
macbethRects[i].Height = (int)(height * 0.09);
if (i % 6 == 0)
macbethRects[i].X = (int)(width * 0.04);
else if (i % 6 == 1)
macbethRects[i].X = (int)(width * 0.21);
else if (i % 6 == 2)
macbethRects[i].X = (int)(width * 0.38);
else if (i % 6 == 3)
macbethRects[i].X = (int)(width * 0.56);
else if (i % 6 == 4)
macbethRects[i].X = (int)(width * 0.72);
else if (i % 6 == 5)
macbethRects[i].X = (int)(width * 0.89);
}
for (int i = 0; i < 24; i++) {
if (i < 6)
macbethRects[i].Y = (int)(height * 0.06);
else if (i < 12)
macbethRects[i].Y = (int)(height * 0.33);
else if (i < 18)
macbethRects[i].Y = (int)(height * 0.58);
else if (i < 24)
macbethRects[i].Y = (int)(height * 0.84);
}
// get patch stats for each rectangle
for (int i = 0; i < 24; i++){
byte[] patch = new byte[macbethRects[0].Width * macbethRects[0].Height*2];
BitmapSource bmpSource = BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray16, null, pixels, width * 2);
//image1.Source = bmpSource;
CroppedBitmap chunk = new CroppedBitmap(bmpSource, macbethRects[i]);
try
{
chunk.CopyPixels(patch, macbethRects[i].Width * 2, 0); // stuff data into 4 pixel (8 byte) array
calculateStats(patch, macbethRects[i].Width, macbethRects[i].Height);
MacbethMean[i] = getMeanValues();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
// calculate average colors
/*int patchrow = 0, patchcol, row;
for (row = 0; row < height; row++) {
if ((row > (0.06 * height)) && (row < (0.15 * height)))
patchrow = 0;
else if ((row > (0.33 * height)) && (row < (0.42 * height)))
patchrow = 1;
else if ((row > (0.58 * height)) && (row < (0.67 * height)))
patchrow = 2;
else if ((row > (0.84 * height)) && (row < (0.93 * height)))
patchrow = 3;
else
patchrow = 4;
for (int col = 0; col < width; col++) {
if ((col > (0.04 * width)) && (col < (0.1 * width)))
patchcol = 0;
else if ((col > (0.21 * width)) && (col < (0.27 * width)))
patchcol = 1;
else if ((col > (0.38 * width)) && (col < (0.44 * width)))
patchcol = 2;
else if ((col > (0.56 * width)) && (col < (0.62 * width)))
patchcol = 3;
else if ((col > (0.72 * width)) && (col < (0.78 * width)))
patchcol = 4;
else if ((col > (0.89 * width)) && (col < (0.95 * width)))
patchcol = 5;
else
patchcol = 6;
if ((patchcol < 6) && (patchrow < 4)) {
meanvalues[patchrow * 6 + patchcol, (row % 2) * 2 + col % 2] += values[row * width + col]; // assign index according to bayer position
pixelcount[patchrow * 6 + patchcol]++;
}
}
}*/
return 0;
}
示例12: CanvImage_MouseMove
/// <summary>
/// Simply grab a 1*1 pixel from the current color image, and
/// use that and copy the new 1*1 image pixels to a byte array and
/// then construct a Color from that.
/// </summary>
private void CanvImage_MouseMove(object sender, MouseEventArgs e)
{
if (!IsMouseDown)
return;
try
{
var cb = new CroppedBitmap(ColorImage.Source as BitmapSource,
new Int32Rect((int) Mouse.GetPosition(CanvImage).X,
(int) Mouse.GetPosition(CanvImage).Y, 1, 1));
var pixels = new byte[4];
try
{
cb.CopyPixels(pixels, 4, 0);
}
catch (Exception)
{
//Ooops
}
//Ok now, so update the mouse cursor position and the SelectedColor
ellipsePixel.SetValue(Canvas.LeftProperty, (Mouse.GetPosition(CanvImage).X - 5));
ellipsePixel.SetValue(Canvas.TopProperty, (Mouse.GetPosition(CanvImage).Y - 5));
CanvImage.InvalidateVisual();
SelectedColor = Color.FromArgb((byte) AlphaSlider.Value, pixels[2], pixels[1], pixels[0]);
}
catch (Exception)
{
//not much we can do
}
}
示例13: GetColour
private Color GetColour(BitmapSource bitmap, int position)
{
if (position >= bitmap.Width - 1)
{
position = (int)bitmap.Width - 2;
}
CroppedBitmap cb = new CroppedBitmap(bitmap, new Int32Rect(position, (int)this.VisualBounds.Height / 2, 1, 1));
byte[] tricolour = new byte[4];
cb.CopyPixels(tricolour, 4, 0);
Color c = Color.FromRgb(tricolour[2], tricolour[1], tricolour[0]);
return c;
}
示例14: getPixelsFromBMPImage
private byte[] getPixelsFromBMPImage(Point loc, int width, int height)
{
byte[] pixels = new byte[width * height * 4]; // allocate mem for current pixel data values
//int[] col = new int[4]; // color values
CroppedBitmap chunk = new CroppedBitmap(bmpSource, new Int32Rect((int)loc.X, (int)loc.Y, width, height)); // get 2x2 region from source
try {
chunk.CopyPixels(pixels, width * 3, 0); // stuff data into 3 byte per pixel
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
return pixels;
}
示例15: MenuSRGBCalibration_Click
private void MenuSRGBCalibration_Click(object sender, RoutedEventArgs e)
{
selection.width = selection.width / 2 * 2;
selection.height = selection.height / 2 * 2;
byte[] pixels = new byte[selection.width * selection.height * 2]; // allocate mem for current pixel data values
int[] values = new int[selection.width * selection.height]; // color values
double[] cumSquareDiff = new double[4];
selection.X = selection.X / 2 * 2; // force even number, otherwise bayer order is not constant
selection.Y = selection.Y / 2 * 2;
CroppedBitmap chunk = new CroppedBitmap(bmpSource, new Int32Rect(selection.X, selection.Y, selection.width, selection.height)); // get 2x2 region from source
try {
chunk.CopyPixels(pixels, selection.width * 2, 0); // stuff data into 4 pixel (8 byte) array
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
stats.calcMacbethStats(pixels, selection.width, selection.height);
imagestats.colorvals[] macbethMeans = new imagestats.colorvals[24];
macbethMeans = stats.getMacbethMean();
}