本文整理汇总了C#中System.Drawing.FastBitmap类的典型用法代码示例。如果您正苦于以下问题:C# FastBitmap类的具体用法?C# FastBitmap怎么用?C# FastBitmap使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FastBitmap类属于System.Drawing命名空间,在下文中一共展示了FastBitmap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetColorCount
/// <summary>
/// Returns the color count from the palette of the given image.
/// </summary>
/// <param name="image">
/// The <see cref="System.Drawing.Image"/> to get the colors from.
/// </param>
/// <returns>
/// The <see cref="int"/> representing the color count.
/// </returns>
public static int GetColorCount(Image image)
{
ConcurrentDictionary<Color, Color> colors = new ConcurrentDictionary<Color, Color>();
int width = image.Width;
int height = image.Height;
using (FastBitmap fastBitmap = new FastBitmap(image))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable once AccessToDisposedClosure
Color color = fastBitmap.GetPixel(x, y);
colors.TryAdd(color, color);
}
});
}
int count = colors.Count;
colors.Clear();
return count;
}
示例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: CountColors
public static int CountColors(this Image image)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
HashSet<int> colors = new HashSet<int>();
FastBitmap fastBitmap = new FastBitmap(image);
fastBitmap.Lock();
// Loop through all the pixels of the image
// and add the colors to the HashSet - duplicates
// are automatically ignored by that collection type
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
// We use ToArgb because Colors are compared on more than their ARGB values - see Color class documentation on MSDN
colors.Add(fastBitmap.GetPixel(x, y).ToArgb());
}
}
fastBitmap.Unlock();
return colors.Count;
}
示例4: 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;
}
示例5: TestFastBitmapCreation
public void TestFastBitmapCreation()
{
Bitmap bitmap = new Bitmap(64, 64);
FastBitmap fastBitmap = new FastBitmap(bitmap);
fastBitmap.Lock();
fastBitmap.Unlock();
// Try creating a FastBitmap with different 32bpp depths
try
{
// ReSharper disable once ObjectCreationAsStatement
new FastBitmap(new Bitmap(1, 1, PixelFormat.Format32bppArgb));
// ReSharper disable once ObjectCreationAsStatement
new FastBitmap(new Bitmap(1, 1, PixelFormat.Format32bppPArgb));
// ReSharper disable once ObjectCreationAsStatement
new FastBitmap(new Bitmap(1, 1, PixelFormat.Format32bppRgb));
}
catch (ArgumentException)
{
Assert.Fail("The FastBitmap should accept any type of 32bpp pixel format bitmap");
}
// Try creating a FastBitmap with a bitmap of a bit depth different from 32bpp
Bitmap invalidBitmap = new Bitmap(64, 64, PixelFormat.Format4bppIndexed);
// ReSharper disable once ObjectCreationAsStatement
new FastBitmap(invalidBitmap);
}
示例6: 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;
}
示例7: Apply
public void Apply(FastBitmap surface, Rectangle[] roi, int startIndex, int length)
{
Rectangle regionBounds = Utility.GetRegionBounds (roi, startIndex, length);
if (regionBounds != Rectangle.Intersect (surface.GetBounds (), regionBounds))
throw new ArgumentOutOfRangeException ("roi", "Region is out of bounds");
unsafe {
for (int x = startIndex; x < startIndex + length; ++x)
ApplyRectangle (surface, roi[x]);
}
}
示例8: RenderEffect
protected unsafe virtual void RenderEffect(FastBitmap src, FastBitmap dst, Rectangle roi)
{
PixelData* src_data_ptr = (PixelData*)src.DataPtr;
int src_width = src.Width;
PixelData* dst_data_ptr = (PixelData*)dst.DataPtr;
int dst_width = dst.Width;
for (int y = roi.Y; y < roi.Bottom; ++y) {
PixelData* srcPtr = src.GetPointAddressUnchecked (src_data_ptr, src_width, roi.X, y);
PixelData* dstPtr = dst.GetPointAddressUnchecked (dst_data_ptr, dst_width, roi.X, y);
RenderLine (srcPtr, dstPtr, roi.Width);
}
}
示例9: ApplyBase
/// <summary>
/// Provides a default implementation for performing dst = F(dst, src) or F(src) over some rectangle
/// of interest. May be slightly faster than calling the other multi-parameter Apply method, as less
/// variables are used in the implementation, thus inducing less register pressure.
/// </summary>
/// <param name="dst">The Surface to write pixels to, and from which pixels are read and used as the lhs parameter for calling the method <b>PixelData Apply(PixelData, PixelData)</b>.</param>
/// <param name="dstOffset">The pixel offset that defines the upper-left of the rectangle-of-interest for the dst Surface.</param>
/// <param name="src">The Surface to read pixels from for the rhs parameter given to the method <b>PixelData Apply(PixelData, PixelData)</b>b>.</param></param>
/// <param name="srcOffset">The pixel offset that defines the upper-left of the rectangle-of-interest for the src Surface.</param>
/// <param name="roiSize">The size of the rectangles-of-interest for all Surfaces.</param>
public void ApplyBase(FastBitmap dst, Point dstOffset, FastBitmap src, Point srcOffset, Size roiSize)
{
// Create bounding rectangles for each Surface
Rectangle dstRect = new Rectangle (dstOffset, roiSize);
if (dstRect.Width == 0 || dstRect.Height == 0)
return;
Rectangle srcRect = new Rectangle (srcOffset, roiSize);
if (srcRect.Width == 0 || srcRect.Height == 0)
return;
// Clip those rectangles to those Surface's bounding rectangles
Rectangle dstClip = Rectangle.Intersect (dstRect, dst.GetBounds ());
Rectangle srcClip = Rectangle.Intersect (srcRect, src.GetBounds ());
// If any of those Rectangles actually got clipped, then throw an exception
if (dstRect != dstClip)
throw new ArgumentOutOfRangeException
(
"roiSize",
"Destination roi out of bounds" +
string.Format (", dst.Size=({0},{1}", dst.Width, dst.Height) +
", dst.Bounds=" + dst.GetBounds ().ToString () +
", dstOffset=" + dstOffset.ToString () +
string.Format (", src.Size=({0},{1}", src.Width, src.Height) +
", srcOffset=" + srcOffset.ToString () +
", roiSize=" + roiSize.ToString () +
", dstRect=" + dstRect.ToString () +
", dstClip=" + dstClip.ToString () +
", srcRect=" + srcRect.ToString () +
", srcClip=" + srcClip.ToString ()
);
if (srcRect != srcClip)
throw new ArgumentOutOfRangeException ("roiSize", "Source roi out of bounds");
// Cache the width and height properties
int width = roiSize.Width;
int height = roiSize.Height;
// Do the work.
unsafe {
for (int row = 0; row < roiSize.Height; ++row) {
PixelData* dstPtr = dst.GetPointAddress (dstOffset.X, dstOffset.Y + row);
PixelData* srcPtr = src.GetPointAddress (srcOffset.X, srcOffset.Y + row);
Apply (dstPtr, srcPtr, width);
}
}
}
示例10: Atlas2dInto1d
//tiles = 16 means 16 x 16 atlas
public List<Bitmap> Atlas2dInto1d(Bitmap atlas2d, int tiles, int atlassizezlimit)
{
FastBitmap orig = new FastBitmap();
orig.bmp = atlas2d;
int tilesize = atlas2d.Width / tiles;
int atlasescount = Math.Max(1, (tiles * tiles * tilesize) / atlassizezlimit);
List<Bitmap> atlases = new List<Bitmap>();
orig.Lock();
//256 x 1
FastBitmap atlas1d = null;
for (int i = 0; i < tiles * tiles; i++)
{
int x = i % tiles;
int y = i / tiles;
int tilesinatlas = (tiles * tiles / atlasescount);
if (i % tilesinatlas == 0)
{
if (atlas1d != null)
{
atlas1d.Unlock();
atlases.Add(atlas1d.bmp);
}
atlas1d = new FastBitmap();
atlas1d.bmp = new Bitmap(tilesize, atlassizezlimit);
atlas1d.Lock();
}
for (int xx = 0; xx < tilesize; xx++)
{
for (int yy = 0; yy < tilesize; yy++)
{
int c = orig.GetPixel(x * tilesize + xx, y * tilesize + yy);
atlas1d.SetPixel(xx, (i % tilesinatlas) * tilesize + yy, c);
}
}
}
atlas1d.Unlock();
atlases.Add(atlas1d.bmp);
orig.Unlock();
return atlases;
}
示例11: 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;
}
示例12: TestSequentialFastBitmapLocking
public void TestSequentialFastBitmapLocking()
{
Bitmap bitmap = new Bitmap(64, 64);
FastBitmap fastBitmap = new FastBitmap(bitmap);
Assert.IsFalse(fastBitmap.Locked, "Immediately after creation, the FastBitmap.Locked property must be false");
fastBitmap.Lock();
Assert.IsTrue(fastBitmap.Locked, "After a successful call to .Lock(), the .Locked property must be true");
fastBitmap.Unlock();
Assert.IsFalse(fastBitmap.Locked, "After a successful call to .Lock(), the .Locked property must be false");
fastBitmap = new FastBitmap(bitmap);
fastBitmap.Lock();
fastBitmap.Unlock();
}
示例13: 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;
}
示例14: CompareColors
public static bool CompareColors(this Image image, Image other)
{
if (image == null || other == null)
{
throw new ArgumentNullException(image == null ? "image" : "other");
}
if (image.Size != other.Size)
{
return false;
}
FastBitmap fastImage = new FastBitmap(image);
FastBitmap fastOther = new FastBitmap(other);
try
{
fastImage.Lock();
fastOther.Lock();
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
// We use ToArgb because Colors are compared on more than their ARGB values - see Color class documentation on MSDN
if (fastImage.GetPixel(x, y).ToArgb() != fastOther.GetPixel(x, y).ToArgb())
{
return false;
}
}
}
}
finally
{
fastImage.Unlock();
fastOther.Unlock();
}
return true;
}
示例15: 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);
}
}
}