向zgke请教关于alpha的问题。

tank_2009 2009-09-08 09:25:51


Bitmap _NewBitmap = new Bitmap(dataGridView1.Width, dataGridView1.Height);

#region
// 为NewBitmap 赋值
#endregion

pictureBox1.Image = myAlpha(_NewBitmap);
pictureBox1.Image.Save(@"c:\1.png");


private Bitmap myAlpha(Bitmap _Bitmap)
{
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);

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(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));

return _Bitmap;
}



我把加入alpha的图像放入到pictureBox中,也没有出现透明。没分了,实在抱歉
...全文
136 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgke 2009-09-08
  • 打赏
  • 举报
回复
恩 这个问题..鼠标事件里 你的先判断需要截图不需要..不需要截图就不执行图形操作..看看
tank_2009 2009-09-08
  • 打赏
  • 举报
回复
好吧...非常感谢 ....

另外 我现在让pictureBox随便着鼠标在DataGridView上面移动。感觉pictureBox显示速度非常慢。
鼠标移动速度稍快,pictureBox就显示不出,请问可以有别的方法来代替,一个图像跟随着鼠标移动,并且效率更高吗?

imageList因为size有限,所以没有考虑。
zgke 2009-09-08
  • 打赏
  • 举报
回复
简单的办法你可以考虑使用 多传递一个参数来实现..
tank_2009 2009-09-08
  • 打赏
  • 举报
回复

float[][] nArray ={ new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 120, 0}, // 120为透明度
new float[] {0, 0, 0, 0, 1}};
ColorMatrix matrix = new ColorMatrix(nArray);
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
Bitmap resultImage = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);

Graphics g = Graphics.FromImage(resultImage);
g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height), 0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height, GraphicsUnit.Pixel, attributes);
pictureBox1.Image = resultImage;




我另外找了一个代码,实现将picturebox中图像变为半透明状,但是能再帮我改一下,和你的那个方法结合,形成渐变为透明的效果。能实现吗?
zgke 2009-09-08
  • 打赏
  • 举报
回复
自己写个更改ALPAH的方法 不知道可以不
tank_2009 2009-09-08
  • 打赏
  • 举报
回复
up............
tank_2009 2009-09-08
  • 打赏
  • 举报
回复
我把这句代码加在 FillRectangle 的后面了

但是还是白色的。
zgke 2009-09-08
  • 打赏
  • 举报
回复
_Graphcis.CompositingMode = CompositingMode.SourceOver;

这样.
tank_2009 2009-09-08
  • 打赏
  • 举报
回复
第2个方法 附值的时候就设置透明

应该怎样做呢?
zgke 2009-09-08
  • 打赏
  • 举报
回复
第1个方法 myAlpha传递背景色.
第2个方法 附值的时候就设置透明
tank_2009 2009-09-08
  • 打赏
  • 举报
回复
赋值确实不是透明的。
tank_2009 2009-09-08
  • 打赏
  • 举报
回复
没有透明吧...PictureBox的背景是透明的。但是赋值后,图像上下应该是橙色。
zgke 2009-09-08
  • 打赏
  • 举报
回复
赋值的时候就已经不是透明的了把
tank_2009 2009-09-08
  • 打赏
  • 举报
回复
tank_2009 2009-09-08
  • 打赏
  • 举报
回复
结贴先...感谢zgke.
zgke 2009-09-08
  • 打赏
  • 举报
回复
这样 最笨的办法

public Bitmap SetAplha(Bitmap p_Bitmap)
{
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);
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, 251, 252, 253);
_SetBruhs.SurroundColors = new Color[] { Color.FromArgb(255, 251, 252, 253) };
_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;

}

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧