110,536
社区成员
发帖
与我相关
我的任务
分享
Bitmap bmp = new Bitmap(100, 100);
using (Graphics g = Graphics.FromImage(bmp))
{
g.FillEllipse(Brushes.Red, new Rectangle(0, 0, 80, 80));
g.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.Green)), new Rectangle(20, 20, 80, 80));
}
e.Graphics.DrawImage(bmp, new Point(10, 10));
//简单的
ControlPaint.DrawImageDisabled(e.Graphics, bmp, 120, 10, Color.White);
//用颜色矩阵
System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
float[][] colorMatrixElements = {
new float[] {.33f, .33f, .33f, 0, 0}, // r = (r+g+b)/3
new float[] {.33f, .33f, .33f, 0, 0}, // g = (r+g+b)/3
new float[] {.33f, .33f, .33f, 0, 0}, // b = (r+g+b)/3
new float[] {0, 0, 0, 1, 0}, // alpha scaling factor of 1
new float[] {0, 0, 0, 0, 1}}; //
System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
attr.SetColorMatrix(matrix);
e.Graphics.DrawImage(bmp, new Rectangle(10, 120, 100, 100), 0, 0, 100, 100, GraphicsUnit.Pixel, attr);
public static Bitmap GetDarkImage(Bitmap bmpSrc) {
Bitmap bmp = bmpSrc.Clone(
new Rectangle(0, 0, bmpSrc.Width, bmpSrc.Height),
PixelFormat.Format24bppRgb //以24位图像作为处理
);
BitmapData bmpData = bmp.LockBits( //锁定位图
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite,
bmp.PixelFormat
);
//计算大小 注意后面不是 width 因为会内存中系统会补齐
byte[] byColorInfo = new byte[bmp.Height * bmpData.Stride];
//将 rgb 数据拷贝到数组中去
Marshal.Copy(bmpData.Scan0, byColorInfo, 0, byColorInfo.Length);
for (int x = 0, xLen = bmp.Width; x < xLen; x++) {
for (int y = 0, yLen = bmp.Height; y < yLen; y++) {
byte byV = GetAvg(//自己写的一个计算平均值的方法
byColorInfo[y * bmpData.Stride + x * 3], //B
byColorInfo[y * bmpData.Stride + x * 3 + 1], //G
byColorInfo[y * bmpData.Stride + x * 3 + 2]); //R
byColorInfo[y * bmpData.Stride + x * 3] =
byColorInfo[y * bmpData.Stride + x * 3 + 1] =
byColorInfo[y * bmpData.Stride + x * 3 + 2] = byV;
}
}
Marshal.Copy(byColorInfo, 0, bmpData.Scan0, byColorInfo.Length);
bmp.UnlockBits(bmpData); //操作完拷贝回去解锁
return bmp;
}
以前的一个 黑白处理的代码 写上了注释 你自己参考吧