C#读写BitMap及颜色相乘
C#读写BitMap及颜色相乘

1 private Bitmap ReadBitMapAndMultipy(Bitmap bitmap0)
2 {
3 int x1width = bitmap0.Width;
4 int y1height = bitmap0.Height;
5 Bitmap image = new Bitmap(x1width, y1height,
6 System.Drawing.Imaging.PixelFormat.Format32bppArgb);
7 int iPixelSize = 4;
8
9 BitmapData bitmapdata = image.LockBits(new
10 Rectangle(0, 0, x1width, y1height),
11 ImageLockMode.ReadWrite, image.PixelFormat);
12 BitmapData bitmapdata0 = bitmap0.LockBits(new
13 Rectangle(0, 0, x1width, y1height),
14 ImageLockMode.ReadOnly, image.PixelFormat);
15 try
16 {
17 unsafe
18 {
19 for (int y = 0; y < y1height; y++)
20 {
21 byte* row = (byte*)bitmapdata.Scan0 +
22 (y * bitmapdata.Stride);
23 byte* row0 = (byte*)bitmapdata0.Scan0 +
24 (y * bitmapdata0.Stride);
25 for (int x = 0; x < x1width; x++)
26 {
27 byte tempValB = row0[x * iPixelSize];
28 byte tempValG = row0[x * iPixelSize + 1];
29 byte tempValR = row0[x * iPixelSize + 2];
30 byte tempValA = row0[x * iPixelSize + 3];
31 double r = Convert.ToDouble(tempValR) * 131 / 255;
32 double g = Convert.ToDouble(tempValG) * 117 / 255;
33 double b = Convert.ToDouble(tempValB) * 80 / 255;
34
35 //double r = Convert.ToDouble(tempValR) * 78 / 255;
36 //double g = Convert.ToDouble(tempValG) * 69 / 255;
37 //double b = Convert.ToDouble(tempValB) * 48 / 255;
38 row[x * iPixelSize] = (byte)(b);
39 row[x * iPixelSize + 1] = (byte)(g);
40 row[x * iPixelSize + 2] = (byte)(r);
41 row[x * iPixelSize + 3] = tempValA;
42 }
43 }
44
45 }
46 }
47 catch
48 {
49 }
50 finally
51 {
52 image.UnlockBits(bitmapdata);
53 }
54 return image;
55 }
调用的代码:
1 string path = System.IO.Path.GetDirectoryName(fileName);
2 Image bitmap = pictureBox1.Image;
3 Bitmap bitmap0 = bitmap as Bitmap;
4 Bitmap sabe = ReadBitMapAndMultipy(bitmap0);
5 Guid guid = new Guid();
6 string file = string.Format(@"{0}\{1}.png", path, guid.ToString());
7 sabe.Save(file);
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
分类:
编程开发集合 / C#开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
2013-04-09 未能加载文件或程序集''file:///D:/Program Files (x86)/ArcGIS/DeveloperKit10.0/DotNet/ESRI.ArcGIS.ADF.Local.dll'' 或它的某一个依赖项。试图加载格式不正确的程序。
2012-04-09 MapControl和PageLayoutControl同步(2)