/// <summary>
/// method for changing the opacity of an image
/// </summary>
/// <param name="image">image to set opacity on</param>
/// <param name="opacity">percentage of opacity</param>
/// <returns></returns>
public System.Drawing.Image SetImageOpacity(System.Drawing.Image image, float opacity)
{
try
{
//create a Bitmap the size of the image provided
Bitmap bmp = new Bitmap(image.Width, image.Height);

//create a graphics object from the image
using (Graphics gfx = Graphics.FromImage(bmp))
{

//create a color matrix object
ColorMatrix matrix = new ColorMatrix();

//set the opacity
matrix.Matrix33 = opacity;

//create image attributes
ImageAttributes attributes = new ImageAttributes();

//set the color(opacity) of the image
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

//now draw the image
gfx.DrawImage(image, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
}
return bmp;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
return null;
}
}

/// <summary>
/// transparent2color
/// </summary>
/// <param name="bmp1"></param>
/// <param name="target"></param>
/// <returns></returns>
Bitmap Transparent2Color(Bitmap bmp1, System.Drawing.Color target)
{
Bitmap bmp2 = new Bitmap(bmp1.Width, bmp1.Height);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp1.Size);
using (Graphics G = Graphics.FromImage(bmp2))
{
G.Clear(target);
G.DrawImageUnscaledAndClipped(bmp1, rect);
}
return bmp2;
}

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-12-13
  • 2021-12-16
  • 2021-12-13
  • 2022-12-23
猜你喜欢
  • 2021-07-23
  • 2021-12-23
  • 2021-12-15
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案