Created
July 21, 2022 14:44
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```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