WinForm中使用Bitmap元素处理图像
创始人
2024-11-11 22:06:45
0

前言

这个Bitmap元素在我们处理图像显示相关时,它的身影就可以见到了。官方术语:封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。 Bitmap 是用于处理由像素数据定义的图像的对象。操作对象最重要的两个方法GetPixel和SetPixel。

一、效果

1、光环效果

2、亮度调节

3、彩色转黑白

4、色温反转

5、马赛克

6、扩散效果

二、代码示例

1、光环效果
 if (bitmap != null)             {                 newbitmap = bitmap.Clone() as Bitmap;                  stopwatch.Restart();                  int width = newbitmap.Width;                 int height = newbitmap.Height;                 float cx = width / 2;                 float cy = height / 2;                 float maxDist = cx * cx + cy * cy;                 float currDist = 0, factor;                 Color pixel;                  for (int i = 0; i < width; i++)                 {                     for (int j = 0; j < height; j++)                     {                         currDist = ((float)i - cx) * ((float)i - cx) + ((float)j - cy) * ((float)j - cy);                         factor = currDist / maxDist;                          pixel = newbitmap.GetPixel(i, j);                         int red = (int)(pixel.R * (1 - factor));                         int green = (int)(pixel.G * (1 - factor));                         int blue = (int)(pixel.B * (1 - factor));                         newbitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));                     }                 }                  stopwatch.Stop();                 lbTimer.Text = stopwatch.ElapsedMilliseconds.ToString() + " ms";                 pictureBox2.Image = newbitmap.Clone() as Image;             }
2、亮度调节
if (bitmap != null)             {                 newbitmap = bitmap.Clone() as Bitmap;                 stopwatch.Reset();                 stopwatch.Restart();                 Color pixel;                 int red, green, blue;                 for (int x = 0; x < newbitmap.Width; x++)                 {                     for (int y = 0; y < newbitmap.Height; y++)                     {                         pixel = newbitmap.GetPixel(x, y);                         red = (int)(pixel.R * 0.6);                         green = (int)(pixel.G * 0.6);                         blue = (int)(pixel.B * 0.6);                         newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));                     }                 }                 stopwatch.Stop();                 lbTimer.Text = stopwatch.ElapsedMilliseconds.ToString() + " ms";                 pictureBox2.Image = newbitmap.Clone() as Image;             }
3、彩色转黑白
if (bitmap != null)             {                 newbitmap = bitmap.Clone() as Bitmap;                 stopwatch.Reset();                 stopwatch.Restart();                 Color pixel;                 int gray;                 for (int x = 0; x < newbitmap.Width; x++)                 {                     for (int y = 0; y < newbitmap.Height; y++)                     {                         pixel = newbitmap.GetPixel(x, y);                         gray = (int)(0.3 * pixel.R + 0.59 * pixel.G + 0.11 * pixel.B);                         newbitmap.SetPixel(x, y, Color.FromArgb(gray, gray, gray));                     }                 }                 stopwatch.Stop();                 lbTimer.Text = stopwatch.ElapsedMilliseconds.ToString() + " ms";                 pictureBox2.Image = newbitmap.Clone() as Image;             }
4、色温反转
 if (bitmap != null)             {                 newbitmap = bitmap.Clone() as Bitmap;                 stopwatch.Restart();                 Color pixel;                 int red, green, blue;                 for (int x = 0; x < newbitmap.Width; x++)                 {                     for (int y = 0; y < newbitmap.Height; y++)                     {                         pixel = newbitmap.GetPixel(x, y);                         red = (int)(255 - pixel.R);                         green = (int)(255 - pixel.G);                         blue = (int)(255 - pixel.B);                         newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));                     }                 }                 stopwatch.Stop();                 lbTimer.Text = stopwatch.ElapsedMilliseconds.ToString() + " ms";                 pictureBox2.Image = newbitmap.Clone() as Image;             }
5、马赛克
if (bitmap != null)             {                 newbitmap = bitmap.Clone() as Bitmap;                 stopwatch.Reset();                 stopwatch.Restart();                 int RIDIO = 50;//马赛克的尺度,默认为周围两个像素                 for (int h = 0; h < newbitmap.Height; h += RIDIO)                 {                     for (int w = 0; w < newbitmap.Width; w += RIDIO)                     {                         int avgRed = 0, avgGreen = 0, avgBlue = 0;                         int count = 0;                         //取周围的像素                         for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)                         {                             for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)                             {                                 Color pixel = newbitmap.GetPixel(x, y);                                 avgRed += pixel.R;                                 avgGreen += pixel.G;                                 avgBlue += pixel.B;                                 count++;                             }                         }                          //取平均值                         avgRed = avgRed / count;                         avgBlue = avgBlue / count;                         avgGreen = avgGreen / count;                          //设置颜色                         for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)                         {                             for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)                             {                                 Color newColor = Color.FromArgb(avgRed, avgGreen, avgBlue);                                 newbitmap.SetPixel(x, y, newColor);                             }                         }                     }                 }                 stopwatch.Stop();                 lbTimer.Text = stopwatch.ElapsedMilliseconds.ToString() + " ms";                 pictureBox2.Image = newbitmap.Clone() as Image;             }
6、扩散效果
if (bitmap != null)             {                 newbitmap = bitmap.Clone() as Bitmap;                 stopwatch.Reset();                 stopwatch.Restart();                 Color pixel;                 int red, green, blue;                 int flag = 0;                 for (int x = 0; x < newbitmap.Width; x++)                 {                     for (int y = 0; y < newbitmap.Height; y++)                     {                         Random ran = new Random();                         int RankKey = ran.Next(-5, 5);                         if (x + RankKey >= newbitmap.Width || y + RankKey >= newbitmap.Height || x + RankKey < 0 || y + RankKey < 0)                         {                             flag = 1;                             continue;                         }                          pixel = newbitmap.GetPixel(x + RankKey, y + RankKey);                         red = (int)(pixel.R);                         green = (int)(pixel.G);                         blue = (int)(pixel.B);                         newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));                     }                 }                 stopwatch.Stop();                 lbTimer.Text = stopwatch.ElapsedMilliseconds.ToString();                 pictureBox2.Image = newbitmap.Clone() as Image;             }

相关内容

热门资讯

开挂透视!hhpoker到底可... 开挂透视!hhpoker到底可以作弊码,微乐贵阳捉鸡麻将挂软件,要领教程(有挂方法)-哔哩哔哩1、全...
昨日!上饶中至辅助软件怎么用,... 昨日!上饶中至辅助软件怎么用,四川麻将血战智能辅助,攻略教程(确实有挂)-哔哩哔哩该软件可以轻松地帮...
黑科技攻略!红龙poker有辅... 黑科技攻略!红龙poker有辅助吗,闲聚app辅助软件,诀窍教程(有挂总结)-哔哩哔哩1、每一步都需...
辅助透视!steampoker... 辅助透视!steampokermaster辅助,微信小程序游戏充值破解,经验教程(有挂透视)-哔哩哔...
据公告内容!微信小游戏修改器,... 据公告内容!微信小游戏修改器,福建微乐小程序修改器,大纲教程(详细教程)-哔哩哔哩1、下载好福建微乐...
透视了解!哈糖大菠萝破解器,宁... 透视了解!哈糖大菠萝破解器,宁德钓螃蟹有没有挂,攻略教程(真实有挂)-哔哩哔哩1、不需要AI权限,帮...
辅助透视!hhpoker真的有... 辅助透视!hhpoker真的有透视吗,微乐小程序游戏破解器苹果系统,窍要教程(了解有挂)-哔哩哔哩1...
据相关数据显示!同乡有辅助,凑... 据相关数据显示!同乡有辅助,凑一桌小程序辅助器,绝活儿教程(有挂实锤)-哔哩哔哩1、上手简单,内置详...
透视系统!poker红龙辅助,... 透视系统!poker红龙辅助,樱花之盛能不能开挂,指引教程(有挂方法)-哔哩哔哩1、完成樱花之盛能不...
辅助透视!pokermaste... 辅助透视!pokermaster辅助器,微信微乐游戏辅助器,妙招教程(详细教程)-哔哩哔哩1、金币登...