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         }
ReadBitMapAndMultipy

调用的代码:

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);
原文地址:https://www.cnblogs.com/yhlx125/p/4409392.html