本文整理汇总了C#中System.Drawing.FastBitmap.SetPixel方法的典型用法代码示例。如果您正苦于以下问题:C# FastBitmap.SetPixel方法的具体用法?C# FastBitmap.SetPixel怎么用?C# FastBitmap.SetPixel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.FastBitmap
的用法示例。
在下文中一共展示了FastBitmap.SetPixel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Alpha
/// <summary>
/// Adjusts the alpha component of the given image.
/// </summary>
/// <param name="source">
/// The <see cref="Image"/> source to adjust.
/// </param>
/// <param name="percentage">
/// The percentage value between 0 and 100 for adjusting the opacity.
/// </param>
/// <param name="rectangle">The rectangle to define the bounds of the area to adjust the opacity.
/// If null then the effect is applied to the entire image.</param>
/// <returns>
/// The <see cref="Bitmap"/> with the alpha component adjusted.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if the percentage value falls outside the acceptable range.
/// </exception>
public static Bitmap Alpha(Image source, int percentage, Rectangle? rectangle = null)
{
if (percentage > 100 || percentage < 0)
{
throw new ArgumentOutOfRangeException(nameof(percentage), "Percentage should be between 0 and 100.");
}
float factor = (float)percentage / 100;
int width = source.Width;
int height = source.Height;
// Traditional examples using a color matrix alter the rgb values also.
using (FastBitmap bitmap = new FastBitmap(source))
{
// Loop through the pixels.
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable AccessToDisposedClosure
Color color = bitmap.GetPixel(x, y);
bitmap.SetPixel(x, y, Color.FromArgb(Convert.ToInt32(color.A * factor), color.R, color.G, color.B));
// ReSharper restore AccessToDisposedClosure
}
});
}
return (Bitmap)source;
}
示例2: ProcessFilter
/// <summary>
/// Processes the given bitmap to apply the threshold.
/// </summary>
/// <param name="source">
/// The image to process.
/// </param>
/// <returns>
/// A processed bitmap.
/// </returns>
public Bitmap ProcessFilter(Bitmap source)
{
int width = source.Width;
int height = source.Height;
using (FastBitmap sourceBitmap = new FastBitmap(source))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable AccessToDisposedClosure
Color color = sourceBitmap.GetPixel(x, y);
sourceBitmap.SetPixel(x, y, color.B >= this.threshold ? Color.White : Color.Black);
// ReSharper restore AccessToDisposedClosure
}
});
}
return source;
}
示例3: ApplyMask
/// <summary>
/// Applies the given image mask to the source.
/// </summary>
/// <param name="source">
/// The source <see cref="Image"/>.
/// </param>
/// <param name="mask">
/// The mask <see cref="Image"/>.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if the two images are of different size.
/// </exception>
/// <returns>
/// The masked <see cref="Bitmap"/>.
/// </returns>
public static Bitmap ApplyMask(Image source, Image mask)
{
if (mask.Size != source.Size)
{
throw new ArgumentException();
}
int width = mask.Width;
int height = mask.Height;
Bitmap toMask = new Bitmap(source);
toMask.SetResolution(source.HorizontalResolution, source.VerticalResolution);
// Loop through and replace the alpha channel
using (FastBitmap maskBitmap = new FastBitmap(mask))
{
using (FastBitmap sourceBitmap = new FastBitmap(toMask))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable AccessToDisposedClosure
Color maskColor = maskBitmap.GetPixel(x, y);
Color sourceColor = sourceBitmap.GetPixel(x, y);
if (sourceColor.A != 0)
{
sourceBitmap.SetPixel(x, y, Color.FromArgb(maskColor.A, sourceColor.R, sourceColor.G, sourceColor.B));
}
// ReSharper restore AccessToDisposedClosure
}
});
}
}
// Ensure the background is cleared out on non alpha supporting formats.
Bitmap clear = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
clear.SetResolution(source.HorizontalResolution, source.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(clear))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImageUnscaled(toMask, 0, 0, width, height);
}
toMask.Dispose();
return clear;
}
示例4: DrawImage
public Bitmap DrawImage(Color[] colors)
{
if (Width < 1 || Height < 1) {
return null;
}
var image = new Bitmap(Width, Height);
var fastBmp = new FastBitmap(image);
fastBmp.LockImage();
for (int y = 0, i = 0; y < Height; y++) {
for (var x = 0; x < Width; x++) {
fastBmp.SetPixel(x, y, colors[(int)Cells[i++].Type]);
}
}
fastBmp.UnlockImage();
return image;
}
示例5: DrawImage
public Bitmap DrawImage(Color[] Colors) {
if (Width < 1 || Height < 1) {
return null;
}
Bitmap Image = new Bitmap(Width, Height);
FastBitmap fastBmp = new FastBitmap(Image);
using (Graphics g = Graphics.FromImage(Image)) {
fastBmp.LockImage();
for (int y = 0, i = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
fastBmp.SetPixel(x, y, Colors[(int)Cells[i++].Type]);
}
}
fastBmp.UnlockImage();
}
return Image;
}
示例6: ProcessFilter
//.........这里部分代码省略.........
tb.TranslateTransform(1, 1);
graphics.FillRectangle(tb, destinationRectangle);
}
}
}
try
{
double[,] horizontalFilter = this.edgeFilter.HorizontalGradientOperator;
int kernelLength = horizontalFilter.GetLength(0);
int radius = kernelLength >> 1;
using (FastBitmap sourceBitmap = new FastBitmap(input))
{
using (FastBitmap destinationBitmap = new FastBitmap(destination))
{
// Loop through the pixels.
Parallel.For(
0,
bufferedHeight,
y =>
{
for (int x = 0; x < bufferedWidth; x++)
{
double rX = 0;
double gX = 0;
double bX = 0;
// Apply each matrix multiplier to the color components for each pixel.
for (int fy = 0; fy < kernelLength; fy++)
{
int fyr = fy - radius;
int offsetY = y + fyr;
// Skip the current row
if (offsetY < 0)
{
continue;
}
// Outwith the current bounds so break.
if (offsetY >= bufferedHeight)
{
break;
}
for (int fx = 0; fx < kernelLength; fx++)
{
int fxr = fx - radius;
int offsetX = x + fxr;
// Skip the column
if (offsetX < 0)
{
continue;
}
if (offsetX < bufferedWidth)
{
// ReSharper disable once AccessToDisposedClosure
Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY);
double r = currentColor.R;
double g = currentColor.G;
double b = currentColor.B;
rX += horizontalFilter[fy, fx] * r;
gX += horizontalFilter[fy, fx] * g;
bX += horizontalFilter[fy, fx] * b;
}
}
}
// Apply the equation and sanitize.
byte red = rX.ToByte();
byte green = gX.ToByte();
byte blue = bX.ToByte();
Color newColor = Color.FromArgb(red, green, blue);
if (y > 0 && x > 0 && y < maxHeight && x < maxWidth)
{
// ReSharper disable once AccessToDisposedClosure
destinationBitmap.SetPixel(x - 1, y - 1, newColor);
}
}
});
}
}
}
finally
{
// We created a new image. Cleanup.
input.Dispose();
}
return destination;
}
示例7: GenerateBitmap
public static void GenerateBitmap(byte[] data, FastBitmap bmp, int startY, int dataAddress, List<Color> fourColors, int addY)
{
int index = 0;
bool onlyHalf = false;
int y = startY;
int x = 0;
int colorZeroValue = fourColors[0].ToArgb();
while (y < bmp.Height && dataAddress + index + 2 < data.Length)
{
int runLength;
int color;
bool restOfLine;
index += DecodeRle(dataAddress + index, data, out color, out runLength, ref onlyHalf, out restOfLine);
if (restOfLine)
runLength = bmp.Width - x;
Color c = fourColors[color]; // set color via the four colors
for (int i = 0; i < runLength; i++, x++)
{
if (x >= bmp.Width - 1)
{
if (y < bmp.Height && x < bmp.Width && c != fourColors[0])
bmp.SetPixel(x, y, c);
if (onlyHalf)
{
onlyHalf = false;
index++;
}
x = 0;
y += addY;
break;
}
if (y < bmp.Height && c.ToArgb() != colorZeroValue)
bmp.SetPixel(x, y, c);
}
}
}
示例8: ApplyFilter
//.........这里部分代码省略.........
angledX = rotatedPoint.X;
angledY = rotatedPoint.Y;
if (rectangle.Contains(new Point(angledX, angledY)))
{
color = sourceBitmap.GetPixel(angledX, angledY);
cmykColor = color;
brushWidth = Math.Min((cmykColor.K / 100f) * multiplier, keylineMax);
// Just using black is too dark.
Brush keylineBrush = new SolidBrush(CmykColor.FromCmykColor(0, 0, 0, cmykColor.K));
graphicsKeyline.FillEllipse(keylineBrush, angledX, angledY, brushWidth, brushWidth);
}
}
}
}
// Set our white background.
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.Clear(Color.White);
}
// Blend the colors now to mimic adaptive blending.
using (FastBitmap cyanBitmap = new FastBitmap(cyan))
using (FastBitmap magentaBitmap = new FastBitmap(magenta))
using (FastBitmap yellowBitmap = new FastBitmap(yellow))
using (FastBitmap keylineBitmap = new FastBitmap(keyline))
using (FastBitmap destinationBitmap = new FastBitmap(newImage))
{
Parallel.For(
offset,
height,
y =>
{
for (int x = offset; x < width; x++)
{
// ReSharper disable AccessToDisposedClosure
Color cyanPixel = cyanBitmap.GetPixel(x, y);
Color magentaPixel = magentaBitmap.GetPixel(x, y);
Color yellowPixel = yellowBitmap.GetPixel(x, y);
Color keylinePixel = keylineBitmap.GetPixel(x, y);
// Negate the offset.
int xBack = x - offset;
int yBack = y - offset;
CmykColor blended = cyanPixel.AddAsCmykColor(magentaPixel, yellowPixel, keylinePixel);
if (rectangle.Contains(new Point(xBack, yBack)))
{
destinationBitmap.SetPixel(xBack, yBack, blended);
}
// ReSharper restore AccessToDisposedClosure
}
});
}
}
padded.Dispose();
cyan.Dispose();
magenta.Dispose();
yellow.Dispose();
keyline.Dispose();
source.Dispose();
source = newImage;
}
catch
{
if (padded != null)
{
padded.Dispose();
}
if (cyan != null)
{
cyan.Dispose();
}
if (magenta != null)
{
magenta.Dispose();
}
if (yellow != null)
{
yellow.Dispose();
}
if (keyline != null)
{
keyline.Dispose();
}
if (newImage != null)
{
newImage.Dispose();
}
}
return source;
}
示例9: Gamma
/// <summary>
/// Adjust the gamma (intensity of the light) component of the given image.
/// </summary>
/// <param name="source">
/// The <see cref="Image"/> source to adjust.
/// </param>
/// <param name="value">
/// The value to adjust the gamma by (typically between .2 and 5).
/// </param>
/// <returns>
/// The <see cref="Bitmap"/> with the gamma adjusted.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if the value falls outside the acceptable range.
/// </exception>
public static Bitmap Gamma(Image source, float value)
{
if (value > 5 || value < .1)
{
throw new ArgumentOutOfRangeException("value", "Value should be between .1 and 5.");
}
int width = source.Width;
int height = source.Height;
Bitmap destination = new Bitmap(width, height);
destination.SetResolution(source.HorizontalResolution, source.VerticalResolution);
byte[] ramp = new byte[256];
for (int x = 0; x < 256; ++x)
{
byte val = ((255.0 * Math.Pow(x / 255.0, value)) + 0.5).ToByte();
ramp[x] = val;
}
using (FastBitmap fastSource = new FastBitmap(source))
{
using (FastBitmap fastDestination = new FastBitmap(destination))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable once AccessToDisposedClosure
Color color = fastSource.GetPixel(x, y);
byte r = ramp[color.R];
byte g = ramp[color.G];
byte b = ramp[color.B];
// ReSharper disable once AccessToDisposedClosure
fastDestination.SetPixel(x, y, Color.FromArgb(color.A, r, g, b));
}
});
}
}
//Rectangle rectangle = new Rectangle(0, 0, width, height);
//using (Graphics graphics = Graphics.FromImage(destination))
//{
// using (ImageAttributes attributes = new ImageAttributes())
// {
// attributes.SetGamma(value);
// graphics.DrawImage(source, rectangle, 0, 0, width, height, GraphicsUnit.Pixel, attributes);
// }
//}
source.Dispose();
return destination;
}
示例10: TestFastBitmapLocker
public void TestFastBitmapLocker()
{
Bitmap bitmap = new Bitmap(64, 64);
FastBitmap fastBitmap = new FastBitmap(bitmap);
// Immediate lock and dispose
fastBitmap.Lock().Dispose();
Assert.IsFalse(fastBitmap.Locked, "After disposing of the FastBitmapLocker object, the underlying fast bitmap must be unlocked");
using (var locker = fastBitmap.Lock())
{
fastBitmap.SetPixel(0, 0, 0);
Assert.AreEqual(fastBitmap, locker.FastBitmap, "The fast bitmap referenced in the fast bitmap locker must be the one that had the original Lock() call");
}
Assert.IsFalse(fastBitmap.Locked, "After disposing of the FastBitmapLocker object, the underlying fast bitmap must be unlocked");
// Test the conditional unlocking of the fast bitmap locker by unlocking the fast bitmap before exiting the 'using' block
using (fastBitmap.Lock())
{
fastBitmap.SetPixel(0, 0, 0);
fastBitmap.Unlock();
}
}
示例11: Trace
/// <summary>
/// Traces the edges of a given <see cref="Image"/>.
/// </summary>
/// <param name="source">
/// The source <see cref="Image"/>.
/// </param>
/// <param name="destination">
/// The destination <see cref="Image"/>.
/// </param>
/// <param name="threshold">
/// The threshold (between 0 and 255).
/// </param>
/// <returns>
/// The a new instance of <see cref="Bitmap"/> traced.
/// </returns>
public static Bitmap Trace(Image source, Image destination, byte threshold = 0)
{
int width = source.Width;
int height = source.Height;
// Grab the edges converting to greyscale, and invert the colors.
ConvolutionFilter filter = new ConvolutionFilter(new SobelEdgeFilter(), true);
using (Bitmap temp = filter.Process2DFilter(source))
{
destination = new InvertMatrixFilter().TransformImage(temp, destination);
// Darken it slightly to aid detection
destination = Adjustments.Brightness(destination, -5);
}
// Loop through and replace any colors more white than the threshold
// with a transparent one.
using (FastBitmap destinationBitmap = new FastBitmap(destination))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable AccessToDisposedClosure
Color color = destinationBitmap.GetPixel(x, y);
if (color.B >= threshold)
{
destinationBitmap.SetPixel(x, y, Color.Transparent);
}
// ReSharper restore AccessToDisposedClosure
}
});
}
// Darken it again to average out the color.
destination = Adjustments.Brightness(destination, -5);
return (Bitmap)destination;
}
示例12: GetImageTransparentPal
public Bitmap GetImageTransparentPal(int index)
{
if (ImagesPal == null || ImagesPal.Count <= index) {
return null;
}
if (IsDrawnPal(index) == false) {
if (DrawPalImage(index) == false) {
throw new Exception("Failed to draw pal-image on index #" + index);
}
}
var img = ImagesPal[index].Image.Clone() as Bitmap;
if (img == null) {
throw new Exception("Invalid pal image on index #" + index);
}
var bg = Palette[0];
var fb = new FastBitmap(img);
fb.LockImage();
for (var x = 0; x < img.Width; x++) {
for (var y = 0; y < img.Height; y++) {
if (fb.GetPixel(x, y) == bg) {
fb.SetPixel(x, y, Color.Transparent);
}
}
}
fb.UnlockImage();
return img;
}
示例13: GetImageTransparentPal
public Bitmap GetImageTransparentPal(int index) {
if (ImagesPal == null || ImagesPal.Count <= index) {
return null;
}
if (IsDrawnPal(index) == false) {
if (DrawPalImage(index) == false) {
// TODO: Exception?
return null;
}
}
Bitmap img = ImagesPal[index].Image.Clone() as Bitmap;
Color bg = Palette[0];
FastBitmap fb = new FastBitmap(img);
fb.LockImage();
for (int x = 0; x < img.Width; x++) {
for (int y = 0; y < img.Height; y++) {
if (fb.GetPixel(x, y) == bg) {
fb.SetPixel(x, y, Color.Transparent);
}
}
}
fb.UnlockImage();
return img;
}
示例14: DrawRgbaImage
public bool DrawRgbaImage(int imageIndex) {
if (ImagesRgba == null || ImagesRgba.Count <= imageIndex) {
return false;
}
if (imageIndex < 0 || imageIndex >= ImagesRgba.Count) {
return false;
}
RoSpriteImageRgba sprImg = ImagesRgba[imageIndex];
if (sprImg == null || sprImg.Data == null || sprImg.Data.Length == 0 || sprImg.Width < 1 || sprImg.Height < 1) {
return false;
}
sprImg.Image = new Bitmap(sprImg.Width, sprImg.Height);
FastBitmap fb = new FastBitmap(sprImg.Image);
int index = 0, alpha = 0, red = 0, green = 0, blue = 0;
Color col;
fb.LockImage();
for (int y = 0; y < sprImg.Height; y++) {
for (int x = 0; x < sprImg.Width; x++, index += 4) {
// A B G R
alpha = sprImg.Data[index];
blue = sprImg.Data[index + 1];
green = sprImg.Data[index + 2];
red = sprImg.Data[index + 3];
col = Color.FromArgb(alpha, red, green, blue);
fb.SetPixel(x, y, col);
}
}
fb.UnlockImage();
return true;
}
示例15: DrawPalImage
public bool DrawPalImage(int imageIndex) {
if (ImagesPal == null || ImagesPal.Count <= imageIndex) {
return false;
}
if (imageIndex < 0 || imageIndex >= ImagesPal.Count) {
return false;
}
RoSpriteImagePal sprImg = ImagesPal[imageIndex];
if (Version >= 0x201 && sprImg.Decoded == false) {
sprImg.Data = RLE.Decode(sprImg.Data);
sprImg.Decoded = true;
}
if (sprImg.Data == null || sprImg.Data.Length == 0 || sprImg.Width < 1 || sprImg.Height < 1) {
return false;
}
sprImg.Image = new Bitmap(sprImg.Width, sprImg.Height);
FastBitmap fb = new FastBitmap(sprImg.Image);
int index;
fb.LockImage();
for (int x = 0; x < sprImg.Width; x++) {
for (int y = 0; y < sprImg.Height; y++) {
index = (x + (y * sprImg.Width));
if (index >= sprImg.Data.Length) {
fb.SetPixel(x, y, Color.Transparent);
continue;
}
fb.SetPixel(x, y, Palette[sprImg.Data[index]]);
}
}
fb.UnlockImage();
return true;
}