本文整理汇总了C#中System.Drawing.Imaging.ImageAttributes.SetColorMatrix方法的典型用法代码示例。如果您正苦于以下问题:C# ImageAttributes.SetColorMatrix方法的具体用法?C# ImageAttributes.SetColorMatrix怎么用?C# ImageAttributes.SetColorMatrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Imaging.ImageAttributes
的用法示例。
在下文中一共展示了ImageAttributes.SetColorMatrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Apply
public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode)
{
Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert);
if (applyRect.Width == 0 || applyRect.Height == 0)
{
// nothing to do
return;
}
GraphicsState state = graphics.Save();
if (Invert)
{
graphics.SetClip(applyRect);
graphics.ExcludeClip(rect);
}
ColorMatrix grayscaleMatrix = new ColorMatrix(new[] {
new[] {.3f, .3f, .3f, 0, 0},
new[] {.59f, .59f, .59f, 0, 0},
new[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
using (ImageAttributes ia = new ImageAttributes())
{
ia.SetColorMatrix(grayscaleMatrix);
graphics.DrawImage(applyBitmap, applyRect, applyRect.X, applyRect.Y, applyRect.Width, applyRect.Height, GraphicsUnit.Pixel, ia);
}
graphics.Restore(state);
}
示例2: DrawEffectImage
public override void DrawEffectImage(Bitmap current, Bitmap next, EffectingPanel effecingPanel)
{
Bitmap doubleBufferingBitmap = null; // ダブルバッファリング用画面
Graphics bg = null; // ダブルバッファリング用画面描画用Graphics
SolidBrush solidBrush = null;
Rectangle rectangle;
ColorMatrix colorMatrix = null;
ImageAttributes imageAttributes = null;
try
{
doubleBufferingBitmap = new Bitmap(current);
bg = Graphics.FromImage(doubleBufferingBitmap);
solidBrush = new SolidBrush(System.Drawing.Color.Black);
rectangle = new Rectangle(0, 0, current.Width, current.Height);
colorMatrix = new ColorMatrix();
imageAttributes = new ImageAttributes();
ResetInterval();
// フェードアウト
for (float alpha = 0.9f; alpha >= 0.0f; alpha -= 0.05f)
{
bg.FillRectangle(solidBrush, rectangle);
colorMatrix.Matrix33 = alpha;
imageAttributes.SetColorMatrix(colorMatrix);
bg.DrawImage(current, rectangle, 0, 0, doubleBufferingBitmap.Width, doubleBufferingBitmap.Height, GraphicsUnit.Pixel, imageAttributes);
Thread.Sleep(10);
effecingPanel.pictureBox.Image = doubleBufferingBitmap;
effecingPanel.pictureBox.Refresh();
DoEventAtIntervals();
}
for (float alpha = 0.0f; alpha <= 1.0f; alpha += 0.05f)
{
bg.FillRectangle(solidBrush, rectangle);
colorMatrix.Matrix33 = alpha;
imageAttributes.SetColorMatrix(colorMatrix);
bg.DrawImage(next, rectangle, 0, 0, doubleBufferingBitmap.Width, doubleBufferingBitmap.Height, GraphicsUnit.Pixel, imageAttributes);
Thread.Sleep(10);
effecingPanel.pictureBox.Image = doubleBufferingBitmap;
effecingPanel.pictureBox.Refresh();
DoEventAtIntervals();
}
bg.Dispose();
}
catch (SystemException ex)
{
Console.WriteLine(ex.Message);
}
}
示例3: GetImage
internal static Image GetImage(Image originalImage, float opacity)
{
Image image = new Bitmap(originalImage);
using (var g = Graphics.FromImage(image)) {
using (var attributes = new ImageAttributes()) {
var matrix = new ColorMatrix { Matrix40 = opacity, Matrix41 = opacity, Matrix42 = opacity };
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
attributes.SetColorMatrix(matrix);
g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
}
}
return image;
}
示例4: ToNegative
/// <summary>
/// To the negative.
/// </summary>
/// <param name="source">The source.</param>
/// <returns></returns>
public static Bitmap ToNegative(this Bitmap source)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(source.Width, source.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
// create the negative color matrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {-1, 0, 0, 0, 0},
new float[] {0, -1, 0, 0, 0},
new float[] {0, 0, -1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {1, 1, 1, 0, 1}
});
// create some image attributes
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height),
0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
示例5: MakeGrayscaleWithColorMatrix
private Bitmap MakeGrayscaleWithColorMatrix(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
示例6: button2_Click
private void button2_Click(object sender, EventArgs e)
{
begin = DateTime.Now.Ticks;
Bitmap currentBitmap = new Bitmap(pictureBox1.Image);
Graphics g = Graphics.FromImage(currentBitmap);
ImageAttributes ia = new ImageAttributes();
float[][] colorMatrix = {
new float[] {0.299f, 0.299f, 0.299f, 0, 0},
new float[] {0.587f, 0.587f, 0.587f, 0, 0},
new float[] {0.114f, 0.114f, 0.114f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
};
ColorMatrix cm = new ColorMatrix(colorMatrix);
ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(currentBitmap, new Rectangle(0, 0, currentBitmap.Width, currentBitmap.Height), 0, 0, currentBitmap.Width, currentBitmap.Height, GraphicsUnit.Pixel, ia);
pictureBox1.Image = currentBitmap;
g.Dispose();
end = DateTime.Now.Ticks;
label1.Text = (end - begin) / 1000 + "毫秒";
}
示例7: SetImageOpacity
public static Image SetImageOpacity(Image image, float opacity)
{
try
{
//create a Bitmap the size of the image provided
var bmp = new Bitmap(image.Width, image.Height);
//create a graphics object from the image
using (var gfx = Graphics.FromImage(bmp))
{
//create a color matrix object
var matrix = new ColorMatrix();
//set the opacity
matrix.Matrix33 = opacity;
//create image attributes
var attributes = new ImageAttributes();
//set the color(opacity) of the image
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//now draw the image
gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height,
GraphicsUnit.Pixel, attributes);
}
return bmp;
}
catch
{
return null;
}
}
示例8: adjustBitmap
/// <summary>
/// change brightness, contrast and/or gamma of a bitmap
/// (see http://stackoverflow.com/questions/15408607/adjust-brightness-contrast-and-gamma-of-an-image)
/// </summary>
/// <param name="originalImage">image to process</param>
/// <param name="brightness">new brightness (1.0 = no changes, 0.0 to 2.0)</param>
/// <param name="contrast">new contrast (1.0 = no changes)</param>
/// <param name="gamma">new gamma (1.0 = no changes)</param>
/// <returns></returns>
static internal Bitmap adjustBitmap(Bitmap originalImage, float brightness = 1.0f, float contrast = 1.0f, float gamma = 1.0f)
{
Bitmap adjustedImage;
ImageAttributes imageAttributes;
Graphics g;
float adjustedBrightness;
adjustedBrightness = brightness - 1.0f;
// create matrix that will brighten and contrast the image
float[][] ptsArray ={
new float[] {contrast, 0, 0, 0, 0}, // scale red
new float[] {0, contrast, 0, 0, 0}, // scale green
new float[] {0, 0, contrast, 0, 0}, // scale blue
new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}};
imageAttributes = new ImageAttributes();
imageAttributes.ClearColorMatrix();
imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
adjustedImage = new Bitmap(originalImage.Width, originalImage.Height);
g = Graphics.FromImage(adjustedImage);
g.DrawImage(originalImage, new Rectangle(0,0,adjustedImage.Width,adjustedImage.Height),
0,0,originalImage.Width,originalImage.Height,
GraphicsUnit.Pixel, imageAttributes);
g.Dispose();
return adjustedImage;
}
示例9: MakeGrayscale
public static Bitmap MakeGrayscale(Bitmap original)
{
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
Graphics g = Graphics.FromImage(newBitmap);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(original, new System.Drawing.Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
g.Dispose();
return newBitmap;
}
示例10: TransformImage
/// <summary>
/// Processes the image.
/// </summary>
/// <param name="source">The current image to process</param>
/// <param name="destination">The new Image to return</param>
/// <returns>
/// The processed <see cref="System.Drawing.Bitmap"/>.
/// </returns>
public override Bitmap TransformImage(Image source, Image destination)
{
using (Graphics graphics = Graphics.FromImage(destination))
{
using (ImageAttributes attributes = new ImageAttributes())
{
attributes.SetColorMatrix(this.Matrix);
Rectangle rectangle = new Rectangle(0, 0, source.Width, source.Height);
graphics.DrawImage(source, rectangle, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);
}
}
// Fade the contrast
destination = Adjustments.Contrast(destination, -25);
// Add a glow to the image.
destination = Effects.Glow(destination, Color.FromArgb(70, 255, 153, 102));
// Add a vignette to finish the effect.
destination = Effects.Vignette(destination, Color.FromArgb(220, 102, 34, 0));
return (Bitmap)destination;
}
示例11: ColorShift
public static Bitmap ColorShift(Image b, Color cs)
{
Bitmap bDest = new Bitmap(b.Width, b.Height, b.PixelFormat);
// Create the ImageAttributes object and apply the ColorMatrix
ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
ColorMatrix shiftMatrix = new ColorMatrix(new float[][]{
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {cs.R/255f, cs.G/255f, cs.B/255f, 0, 1}
});
attributes.SetColorMatrix(shiftMatrix);
// Use a new Graphics object from the new image.
using (Graphics g = Graphics.FromImage(bDest))
{
// Draw the original image using the ImageAttributes created above.
g.DrawImage(b,
new Rectangle(0, 0, b.Width, b.Height),
0, 0, b.Width, b.Height,
GraphicsUnit.Pixel,
attributes);
}
return bDest;
}
示例12: Brightness
public static Bitmap Brightness(Image b, float brightness)
{
Bitmap bDest = new Bitmap(b.Width, b.Height, b.PixelFormat);
float adjustedBrightness = brightness - 1.0f;
// Create the ImageAttributes object and apply the ColorMatrix
ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
ColorMatrix brightnessMatrix = new ColorMatrix(new float[][]{
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {brightness, brightness, brightness, 0, 1}
});
attributes.SetColorMatrix(brightnessMatrix);
// Use a new Graphics object from the new image.
using (Graphics g = Graphics.FromImage(bDest))
{
// Draw the original image using the ImageAttributes created above.
g.DrawImage(b,
new Rectangle(0, 0, b.Width, b.Height),
0, 0, b.Width, b.Height,
GraphicsUnit.Pixel,
attributes);
}
return bDest;
}
示例13: ImageToGrayscale
/// <summary>
/// Converts image to a grayscale image
/// </summary>
public static Image ImageToGrayscale(Image image)
{
try
{
Bitmap bitmap = new Bitmap(image.Width, image.Height);
Graphics graphics = Graphics.FromImage(bitmap);
ColorMatrix matrix = new ColorMatrix(new float[][]{
new float[]{0.3f,0.3f,0.3f,0,0},
new float[]{0.59f,0.59f,0.59f,0,0},
new float[]{0.11f,0.11f,0.11f,0,0},
new float[]{0,0,0,1,0,0},
new float[]{0,0,0,0,1,0},
new float[]{0,0,0,0,0,1}});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix);
graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
graphics.Dispose(); // Dispose temp graphics
return (Image)bitmap;
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
return image;
}
}
示例14: InvertImage
internal static Image InvertImage(Image myImage)
{
var invertedBmp = new Bitmap(myImage.Width, myImage.Height);
//Setup color matrix
var clrMatrix =
new ColorMatrix(new[]
{
new float[] {-1, 0, 0, 0, 0}, new float[] {0, -1, 0, 0, 0}, new float[] {0, 0, -1, 0, 0},
new float[] {0, 0, 0, 1, 0}, new float[] {1, 1, 1, 0, 1}
});
using (var attr = new ImageAttributes())
{
//Attach matrix to image attributes
attr.SetColorMatrix(clrMatrix);
using (var g = Graphics.FromImage(invertedBmp))
{
g.DrawImage(myImage, new Rectangle(0, 0, myImage.Width, myImage.Height), 0, 0, myImage.Width,
myImage.Height, GraphicsUnit.Pixel, attr);
}
}
return invertedBmp;
}
示例15: Perform
public override Bitmap Perform(Bitmap bitmap)
{
float contrastAdjusted = Contrast / 1000f + 1.0f;
EnsurePixelFormat(ref bitmap);
using (var g = Graphics.FromImage(bitmap))
{
var attrs = new ImageAttributes();
attrs.SetColorMatrix(new ColorMatrix
{
Matrix00 = contrastAdjusted,
Matrix11 = contrastAdjusted,
Matrix22 = contrastAdjusted
});
g.DrawImage(bitmap,
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0,
0,
bitmap.Width,
bitmap.Height,
GraphicsUnit.Pixel,
attrs);
}
return bitmap;
}