Skip to content

Instantly share code, notes, and snippets.

@arcanisgk
Created July 21, 2022 14:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save arcanisgk/94936463da6288bdc131be585b1b96b8 to your computer and use it in GitHub Desktop.
```C#
namespace CreateImageClass
{
public sealed class ImageControlOnFly
{
public Main MainForm;
public Bitmap Bmp = new Bitmap(1, 1);
internal void StartTrackCoordinates()
{
CreateImageOverlay();
}
private void CreateImageOverlay()
{
getImage();
SetImageOpacity((float)0.9);
PictureBox picBox = new PictureBox();
picBox.BorderStyle = BorderStyle.None;
picBox.Image = Bmp;
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
picBox.Name = "pictAction";
picBox.Dock = DockStyle.Fill;
MainForm.Controls.Add(picBox);
PictureBox picBoxControl = MainForm.Controls["pictAction"] as PictureBox;
picBoxControl.BringToFront();
}
private void getImage()
{
using (Graphics gfx = Graphics.FromImage(Bmp))
using (SolidBrush brush = new SolidBrush(Color.Violet))
{
gfx.FillRectangle(brush, 0, 0, 1, 1);
}
}
public void SetImageOpacity(float opacity)
{
try
{
//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(Bmp, new Rectangle(0, 0, Bmp.Width, Bmp.Height), 0, 0, Bmp.Width, Bmp.Height, GraphicsUnit.Pixel, attributes);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment