使用方式
[C#] 纯文本查看 复制代码 Bitmap _Bitmap = (Bitmap)Image.FromFile(@"c:/1.jpg");
pictureBox3.Image = _Bitmap;
Bitmap _NewBitmap1 = Zgke.MyImage.Panit.Impression.BothAlpha(_Bitmap, false, false);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = _NewBitmap1;
Bitmap _NewBitmap2 = Zgke.MyImage.Panit.Impression.BothAlpha(_Bitmap, false, true);
pictureBox2.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox2.Image = _NewBitmap2;
效果图
下面是全部代码[C#] 纯文本查看 复制代码 /// <summary>
/// 设置图形边缘半透明
/// </summary>
/// <param name="p_Bitmap">图形</param>
/// <param name="p_CentralTransparent">true中心透明 false边缘透明</param>
/// <param name="p_Crossdirection">true横 false纵</param>
/// <returns></returns>
public static Bitmap BothAlpha(Bitmap p_Bitmap, bool p_CentralTransparent, bool p_Crossdirection) {
Bitmap _SetBitmap = new Bitmap(p_Bitmap.Width, p_Bitmap.Height);
Graphics _GraphisSetBitmap = Graphics.FromImage(_SetBitmap);
_GraphisSetBitmap.DrawImage(p_Bitmap, new Rectangle(0, 0, p_Bitmap.Width, p_Bitmap.Height));
_GraphisSetBitmap.Dispose();
Bitmap _Bitmap = new Bitmap(_SetBitmap.Width, _SetBitmap.Height);
Graphics _Graphcis = Graphics.FromImage(_Bitmap);
Point _Left1 = new Point(0, 0);
Point _Left2 = new Point(_Bitmap.Width, 0);
Point _Left3 = new Point(_Bitmap.Width, _Bitmap.Height / 2);
Point _Left4 = new Point(0, _Bitmap.Height / 2);
if (p_Crossdirection) {
_Left1 = new Point(0, 0);
_Left2 = new Point(_Bitmap.Width / 2, 0);
_Left3 = new Point(_Bitmap.Width / 2, _Bitmap.Height);
_Left4 = new Point(0, _Bitmap.Height);
}
Point[] _Point = new Point[] { _Left1, _Left2, _Left3, _Left4 };
PathGradientBrush _SetBruhs = new PathGradientBrush(_Point, WrapMode.TileFlipY);
_SetBruhs.CenterPoint = new PointF(0, 0);
_SetBruhs.FocusScales = new PointF(_Bitmap.Width / 2, 0);
_SetBruhs.CenterColor = Color.FromArgb(0, 255, 255, 255);
_SetBruhs.SurroundColors = new Color[] { Color.FromArgb(255, 255, 255, 255) };
if (p_Crossdirection) {
_SetBruhs.FocusScales = new PointF(0, _Bitmap.Height);
_SetBruhs.WrapMode = WrapMode.TileFlipX;
}
if (p_CentralTransparent) {
_SetBruhs.CenterColor = Color.FromArgb(255, 255, 255, 255);
_SetBruhs.SurroundColors = new Color[] { Color.FromArgb(0, 255, 255, 255) };
}
_Graphcis.FillRectangle(_SetBruhs, new Rectangle(0, 0, _Bitmap.Width, _Bitmap.Height));
_Graphcis.Dispose();
BitmapData _NewData = _Bitmap.LockBits(new Rectangle(0, 0, _Bitmap.Width, _Bitmap.Height), ImageLockMode.ReadOnly, _Bitmap.PixelFormat);
byte[] _NewBytes = new byte[_NewData.Stride * _NewData.Height];
Marshal.Copy(_NewData.Scan0, _NewBytes, 0, _NewBytes.Length);
_Bitmap.UnlockBits(_NewData);
BitmapData _SetData = _SetBitmap.LockBits(new Rectangle(0, 0, _SetBitmap.Width, _SetBitmap.Height), ImageLockMode.ReadWrite, _SetBitmap.PixelFormat);
byte[] _SetBytes = new byte[_SetData.Stride * _SetData.Height];
Marshal.Copy(_SetData.Scan0, _SetBytes, 0, _SetBytes.Length);
int _WriteIndex = 0;
for (int i = 0; i != _SetData.Height; i++) {
_WriteIndex = i * _SetData.Stride + 3;
for (int z = 0; z != _SetData.Width; z++) {
_SetBytes[_WriteIndex] = _NewBytes[_WriteIndex];
_WriteIndex += 4;
}
}
Marshal.Copy(_SetBytes, 0, _SetData.Scan0, _SetBytes.Length);
_SetBitmap.UnlockBits(_SetData);
return _SetBitmap;
}
|
|