/
BitmapHelper.cs
44 lines (41 loc) · 1.02 KB
/
BitmapHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace Free.Controls.TreeView
{
public static class BitmapHelper
{
[StructLayout(LayoutKind.Sequential)]
private struct PixelData
{
public byte B;
public byte G;
public byte R;
public byte A;
}
public static void SetAlphaChanelValue(Bitmap image, byte value)
{
if(image==null)
throw new ArgumentNullException("image");
if(image.PixelFormat!=PixelFormat.Format32bppArgb)
throw new ArgumentException("Wrong PixelFormat");
BitmapData bitmapData=image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
PixelData* pPixel=(PixelData*)bitmapData.Scan0;
for(int i=0; i<bitmapData.Height; i++)
{
for(int j=0; j<bitmapData.Width; j++)
{
pPixel->A=value;
pPixel++;
}
pPixel+=bitmapData.Stride-(bitmapData.Width*4);
}
}
image.UnlockBits(bitmapData);
}
}
}