I want to save all drawstring which is done with the codes at the bottom half. I am saving it by saving the "Points" in a List type. The purpose of saving is because I want to have the ability to delete a particular drawing. All other drawings will be retained and only the one which wants to be deleted will be removed. My main query is why can't I use the same code with some minor editing(Top half of the code is the code I use to add new drawstring) that I use to draw to redraw when I am deleting a particular drawing.
Side_pictureBox.ImageLocation = AppDomain.CurrentDomain.BaseDirectory + #"pictures for app\Bus_Nearside.png";
Side_pictureBox.Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + #"\pictures for app\Bus_Nearside.png");
Bitmap bm = new Bitmap(Side_pictureBox.Image);
if (Tagged_Remarks_listBox.SelectedIndex == 0)
{
for (int x = 0; x <= NumberingPosition.Count - 1; x++)
{
if (x != 0)
{
using (Graphics gr = Graphics.FromImage(bm))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
Font drawFont = new Font("Calibri (Body)", 15);
SolidBrush drawBrush = new SolidBrush(Color.Blue);
//MessageBox.Show(Numbering[u] + NumberingPosition[u]);
gr.DrawString(Numbering[x], drawFont, drawBrush, NumberingPosition[x]);
}
}
Side_pictureBox.Image = bm;
Side_pictureBox.Invalidate();
}
//Above code is when I first drawstring ,Below Code is to redraw when deleting particular drawing//
Bitmap bm = new Bitmap(Side_pictureBox.Image);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
String drawString = numbering_for_digram.ToString();
Font drawFont = new Font("Calibri (Body)", 15);
SolidBrush drawBrush = new SolidBrush(Color.Blue);
gr.DrawString(drawString, drawFont, drawBrush, lastPoint);
Numbering.Add(drawString);
drawFont.Dispose();
drawBrush.Dispose();
}
Side_pictureBox.Image = bm;
If we're in the Paint() event, then I'd expect to see something more like this:
if (Tagged_Remarks_listBox.SelectedIndex == 0)
{
Graphics gr = e.Graphics;
gr.SmoothingMode = SmoothingMode.AntiAlias;
Font drawFont = new Font("Calibri (Body)", 15);
SolidBrush drawBrush = new SolidBrush(Color.Blue);
for (int x = 1; x <= NumberingPosition.Count - 1; x++)
{
//MessageBox.Show(Numbering[u] + NumberingPosition[u]);
gr.DrawString(Numbering[x], drawFont, drawBrush, NumberingPosition[x]);
}
drawFont.Dispose();
drawBrush.Dispose();
}
Note that calling Invalidate() in the Paint() would cause it to repaint itself REPEATEDLY and FOREVER...which might be part of the problem.
I found a way to make it work(Maybe not the best, most effective, efficient way), by using Bitmap to take in all the drawings. This bitmap will then be saved as a Png file. Once all drawings are done, I declare the picturebox.image as the bitmap's Png file.
private void Side_pictureBox_Paint(object sender, PaintEventArgs e)
{
if (Tagged_Remarks_listBox.SelectedIndex == 0 && selectedindexreset == true)
{
//Side_pictureBox.ImageLocation = AppDomain.CurrentDomain.BaseDirectory + #"pictures for app\Bus_Nearside.png";
Side_pictureBox.Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + #"\pictures for app\Bus_Nearside.png");
Bitmap bmforedit = new Bitmap(Side_pictureBox.Image);
//MessageBox.Show("inside");
using (Graphics gr = Graphics.FromImage(bmforedit))
{
for (int x = 0; x <= NumberingPosition.Count - 1; x++)
{
//MessageBox.Show(x.ToString());
if (Numbering[x] != "1") // change accordingly
{
//MessageBox.Show(Numbering[x]);
gr.SmoothingMode = SmoothingMode.AntiAlias;
Font drawFont = new Font("Calibri (Body)", 15);
SolidBrush drawBrush = new SolidBrush(Color.Blue);
//MessageBox.Show(Numbering[x] + NumberingPosition[x]);
gr.DrawString(Numbering[x], drawFont, drawBrush, NumberingPosition[x]);
drawFont.Dispose();
drawBrush.Dispose();
}
}
// bmforedit.Save(#"C:\Users\user\Desktop\PDI_APP_EDIT_FOR_TO\PDIPROTOTYPE2\bin\Debug\pictures for app\TestImage.png");
Side_pictureBox.Image.Dispose();
//bmforedit.Save(#"C:\Users\user\Desktop\PDI_APP_EDIT_FOR_TO\PDIPROTOTYPE2\bin\Debug\pictures for app\TestImage1.png");
Side_pictureBox.Image = bmforedit;
}
for (int u = 0; u <= PrevStore.Count - 1; u++)
{
using (Graphics g = Graphics.FromImage(bmforedit))
{
if (u < StartDrawCount[0] || u > StopDrawCount[0])
{
g.DrawLine(new Pen(Color.DarkRed, 2), PrevStore[u], NowStore[u]);
g.SmoothingMode = SmoothingMode.AntiAlias;
}
}
}
bmforedit.Save(#"C:\Users\user\Desktop\PDI_APP_EDIT_FOR_TO\PDIPROTOTYPE2\bin\Debug\pictures for app\TestImage2.png");
selectedindexreset = false;
Side_pictureBox.ImageLocation = AppDomain.CurrentDomain.BaseDirectory + #"pictures for app\TestImage2.png";
Side_pictureBox.Refresh();
}
}
Related
Okay, to start off. I'm trying to make dynamic editable, addable, and removeable text onto a picturebox. I got that working.
When saving an image from a picturebox, it doesn't save the labels. I now got it to draw the labels as a string using Graphics. Yet, it only draws the last modified/added/edited label to the pictureBox. I'm lost.
Here's my code for drawing the labels & saving them:
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ext = Path.GetExtension(sfd.FileName);
switch (ext)
{
case ".jpg":
format = ImageFormat.Jpeg;
break;
case ".bmp":
format = ImageFormat.Bmp;
break;
}
Bitmap bmp = new Bitmap(pictureBox1.Image);
RectangleF rectf = new RectangleF(70, 90, 90, 50);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.Flush();
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
SolidBrush brush = new SolidBrush(label.ForeColor);
for (int i = 0; i < n; i++)
{
g.DrawString(label.Text, label.Font, brush, label.Location);
label.SelectNextControl(label, false, false, true, false);
}
pictureBox1.Image = bmp;
pictureBox1.Image.Save(sfd.FileName, format);
}
Here's where the labels are defined and created:
label = new CustomLabel();
label.Name = "" + n;
label.Location = new Point(newTextbox.Location.X, newTextbox.Location.Y);
label.Text = newTextbox.Text;
label.Font = new Font("Verdana", fontSize);
label.BackColor = Color.Transparent;
label.ForeColor = textColor;
label.AutoSize = true;
label.Visible = true;
newTextbox.Visible = false;
newTextbox.Dispose();
pictureBox1.Controls.Add(label);
TextSelected = false;
label.DoubleClick += new System.EventHandler(this.label_DoubleClick);
label.MouseDown += new MouseEventHandler(this.label_MouseDown);
label.MouseUp += new MouseEventHandler(this.MouseUp);
label.MouseMove += new MouseEventHandler(this.MouseMove);
n++;
And n is defined:
public int n = 1;
Where the stroke is added to the text:
public class CustomLabel : Label
{
public CustomLabel()
{
OutlineForeColor = Color.Black;
OutlineWidth = 3;
}
public Color OutlineForeColor { get; set; }
public float OutlineWidth { get; set; }
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
using (GraphicsPath gp = new GraphicsPath())
using (Pen outline = new Pen(OutlineForeColor, OutlineWidth)
{ LineJoin = LineJoin.Round })
using (StringFormat sf = new StringFormat())
using (Brush foreBrush = new SolidBrush(ForeColor))
{
gp.AddString(Text, Font.FontFamily, (int)Font.Style,
Font.Size, ClientRectangle, sf);
e.Graphics.ScaleTransform(1.3f, 1.35f);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.DrawPath(outline, gp);
e.Graphics.FillPath(foreBrush, gp);
}
}
}
The problem is in your for loop:
for (int i = 0; i < n; i++)
{
g.DrawString(label.Text, label.Font, brush, label.Location);
label.SelectNextControl(label, false, false, true, false);
}
Here label never changes, so you are just drawing the same label n times. And I don't know what SelectNextControl does.
I suggest looping over the controls in the picture box:
foreach (var customLabel in pictureBox1.Controls.OfType<CustomLabel>()) {
g.DrawString(customLabel.Text, customLabel.Font, brush, customLabel.Location);
}
I am having some issues rotating the text on a tab. The tabs originally worked just fine, but then I wanted to bold the text when selected, so I used the Draw Item Event. I added a RotateTransform and a TranslateTransform, but its not working. The text just doesn't show up. I have troubleshot it and if I take the rotation away, the text is visible, but when I use the rotate to make the text vertical, it disappears. Here's my code:
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
string tabText = tabControl1.TabPages[e.Index].Text;
SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
Brush _textBrush = Brushes.Black;
TabPage _tabPage = tabControl1.TabPages[e.Index];
System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
StringFormat _stringFlags = new StringFormat();
_stringFlags.Alignment = StringAlignment.Center;
_stringFlags.LineAlignment = StringAlignment.Center;
PointF tabPt = new PointF(_tabBounds.Left+(_tabBounds.Width), _tabBounds.Top+(_tabBounds.Height));
if (e.Index == tabControl1.SelectedIndex)
{
g.RotateTransform(-90);
g.TranslateTransform(tabPt.X, tabPt.Y);
g.DrawString(tabControl1.TabPages[e.Index].Text,
new Font(tabControl1.Font, FontStyle.Bold),
_textBrush,
new PointF(tabPt.X, tabPt.Y));
g.ResetTransform();
}
else
{
g.TranslateTransform(tabPt.X, tabPt.Y);
g.RotateTransform(-90);
g.DrawString(tabControl1.TabPages[e.Index].Text,
tabControl1.Font,
_textBrush,
new PointF(tabPt.X,tabPt.Y));
g.ResetTransform();
}
}
}
Any help would be greatly appreciated.
EDIT
Here's the image:
Here's the new code:
Graphics g = e.Graphics;
string tabText = tabControl1.TabPages[e.Index].Text;
SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
Brush _textBrush = Brushes.Black;
TabPage _tabPage = tabControl1.TabPages[e.Index];
System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
PointF rotPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (2.75F), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width/2));
PointF tabPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (2.75F), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width)/2);
if (e.Index == tabControl1.SelectedIndex)
{
g.TranslateTransform(rotPt.X, rotPt.Y);
g.RotateTransform(-90);
g.TranslateTransform(-(rotPt.X), -(rotPt.Y));
g.DrawString(tabText,
new Font(tabControl1.Font, FontStyle.Bold),
_textBrush,
new PointF(rotPt.X, rotPt.Y));
}
else
{
g.TranslateTransform(rotPt.X, rotPt.Y);
g.RotateTransform(-90);
g.TranslateTransform(-rotPt.X, -rotPt.Y);
g.DrawString(tabText,
tabControl1.Font,
_textBrush,
new PointF(rotPt.X,rotPt.Y));
}
Thanks TaW for your help. Here's the final code and its working.
public form1()
{
InitializeComponent();
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
string tabText = tabControl1.TabPages[e.Index].Text;
SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
Brush _textBrush = Brushes.Black;
TabPage _tabPage = tabControl1.TabPages[e.Index];
System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
PointF rotPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (textSize.Height / 2), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width/2));
if (e.State.HasFlag(DrawItemState.Selected))
{
g.TranslateTransform(rotPt.X, rotPt.Y);
g.RotateTransform(-90);
g.TranslateTransform(-(rotPt.X), -(rotPt.Y));
g.DrawString(tabText,
new Font(tabControl1.Font, FontStyle.Bold),
_textBrush,
new PointF(rotPt.X, rotPt.Y));
g.ResetTransform();
}
else
{
g.TranslateTransform(rotPt.X, rotPt.Y);
g.RotateTransform(-90);
g.TranslateTransform(-rotPt.X, -rotPt.Y);
g.DrawString(tabText,
tabControl1.Font,
_textBrush,
new PointF(rotPt.X,rotPt.Y));
g.ResetTransform();
}
}
P.S. I never did get the e.Bounds rather than GetTabRect (i'm not sure how to set it to the selected tab).
I can filter red and blue color separately but i want to filter both at the same time.. so my code is like this
//FOR RED COLOR
ColorFiltering filter = new ColorFiltering();
filter.Red = new IntRange(100, 255);
filter.Green = new IntRange(0, 75);
filter.Blue = new IntRange(0, 75);
filter.ApplyInPlace(image1);
MyDraw(image1);
// FOR BLUE COLOR
EuclideanColorFiltering filter2 = new EuclideanColorFiltering();
filter2.CenterColor = new RGB(Color.FromArgb(9, 39, 101));
filter2.Radius = 50;
filter2.ApplyInPlace(image1);
MyDraw(image1);
public void MyDraw(Bitmap image)
{
BlobCounter blobCounter = new BlobCounter();
blobCounter.MinWidth = 2;
blobCounter.MinHeight = 2;
blobCounter.FilterBlobs = true;
blobCounter.ObjectsOrder = ObjectsOrder.Size;
Grayscale grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
Bitmap grayImage = grayFilter.Apply(image);
blobCounter.ProcessImage(grayImage);
Rectangle[] rects = blobCounter.GetObjectsRectangles();
foreach (Rectangle recs in rects)
{
if (rects.Length > 0)
{
Rectangle objectRect = rects[0];
//Graphics g = Graphics.FromImage(image);
Graphics g = pictureBox1.CreateGraphics();
reception = "Cam," + objectRect.X + "," + objectRect.Y;
Console.WriteLine("X: " + objectRect.X + " Y:" + objectRect.Y.ToString());
using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
{
g.DrawRectangle(pen, objectRect);
}
int objectX = objectRect.X + (objectRect.Width / 2);
int objectY = objectRect.Y + (objectRect.Height / 2);
g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1));
g.Dispose();
}
}
}
So i want to recognize blue and red shapes on webcam and draw a rectangle around recognized shape. For now, I can do it as red or blue. But I want to recognize red and blue colors at the same time
how can i add multiple filters?
I want to auto crop the image which I took it from tablet camera. Actually I want to take the picture of id cards and need to auto corp it, I am using black background but sometimes it is too glaze and there is a reflection also, at that time its not cropped properly. Can anyone assist me with c# code to crop the images(Note : I am using phone cameras to take the image of id cards, mostly the background will be black and sometime it will be some other things).
Below I mentioned my code also:
private void ProcessImage(Bitmap bitmap)
{
// lock image
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, bitmap.PixelFormat);
// step 1 - turn background to black
ColorFiltering colorFilter = new ColorFiltering();
colorFilter.Red = new IntRange(0, 64);
colorFilter.Green = new IntRange(0, 64);
colorFilter.Blue = new IntRange(0, 64);
colorFilter.FillOutsideRange = false;
colorFilter.ApplyInPlace(bitmapData);
// step 2 - locating objects
BlobCounter blobCounter = new BlobCounter();
blobCounter.FilterBlobs = true;
blobCounter.MinHeight = 5;
blobCounter.MinWidth = 5;
blobCounter.ProcessImage(bitmapData);
Blob[] blobs = blobCounter.GetObjectsInformation();
bitmap.UnlockBits(bitmapData);
// step 3 - check objects' type and highlight
SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
Graphics g = Graphics.FromImage(bitmap);
Pen yellowPen = new Pen(Color.Yellow, 2); // circles
Pen redPen = new Pen(Color.Red, 2); // quadrilateral
Pen brownPen = new Pen(Color.Brown, 2); // quadrilateral with known sub-type
Pen greenPen = new Pen(Color.Green, 2); // known triangle
Pen bluePen = new Pen(Color.Blue, 2); // triangle
for (int i = 0, n = blobs.Length; i < n; i++)
{
List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
DoublePoint center;
double radius;
List<IntPoint> corners;
// is triangle or quadrilateral
if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
{
// get sub-type
PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
Pen pen;
if (subType == PolygonSubType.Rectangle)
{
pen = (corners.Count == 4) ? redPen : bluePen;
}
else
{
pen = (corners.Count == 4) ? brownPen : greenPen;
}
//g.DrawPolygon( pen, ToPointsArray( corners ) );
if (pen.Color.Name == "Red")
{
int intdummy = 0;
g.DrawRectangle(pen, corners[0].X, corners[0].Y, 1150, 750);
//Code for corpping///////
pictureBox2.Image = bitmap;
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap croppedBitmap = new Bitmap("E:\\xxx\\Vicas Tablet\\WinImager\\cropimage\\cropimage\\Testing Images\\Image1.jpg");
//croppedBitmap = croppedBitmap.Clone(new Rectangle(corners[0].X, corners[0].Y,1150,750),System.Drawing.Imaging.PixelFormat.DontCare);
croppedBitmap = croppedBitmap.Clone(
new Rectangle(corners[0].X, corners[0].Y, ((int)corners[1].X - (int)corners[0].X), ((int)corners[2].Y - (int)corners[1].Y)), System.Drawing.Imaging.PixelFormat.DontCare);
pictureBox2.Image = croppedBitmap;
////End code for cropping///////////
}
}
Building a little paint program and am trying to incorporate the concept of layers.
I'm using a PictureBox control to display the image, and getting the Graphics object from the image being displayed by the PictureBox and drawing to that.
My problem is I'm trying to figure out how to draw to a new Graphics object that is overlayed on top of the picture box, and be able to get the newly drawn image without the original image absorbed into the graphic.
If I do something like:
Graphics gr = Graphics.FromImage(myPictureBox.image);
gr.DrawRectangle(blah blah)
...I am editing the original image in the picture box. I want a way to only capture the new stuff being drawn as a separate image, but still have it displayed as an overlay over top of what was already there.
Anyone able to point me in the right direction? Thanks!
I would reckon to use the transparent control and do some modification so it can be used as image layers:
http://www.codeproject.com/Articles/26878/Making-Transparent-Controls-No-Flickering
Probably something like this (make any modification as necessary).
class LayerControl : UserControl
{
private Image image;
private Graphics graphics;
public LayerControl(int width, int height)
{
this.Width = width;
this.Height = height;
image = new Bitmap(width, height);
graphics = Graphics.FromImage(image);
// Set style for control
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
}
// this function will draw your image
protected override void OnPaint(PaintEventArgs e)
{
var bitMap = new Bitmap(image);
// by default the background color for bitmap is white
// you can modify this to follow your image background
// or create a new Property so it can dynamically assigned
bitMap.MakeTransparent(Color.White);
image = bitMap;
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.GammaCorrected;
float[][] mtxItens = {
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,1,0},
new float[] {0,0,0,0,1}};
ColorMatrix colorMatrix = new ColorMatrix(mtxItens);
ImageAttributes imgAtb = new ImageAttributes();
imgAtb.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
g.DrawImage(image,
ClientRectangle,
0.0f,
0.0f,
image.Width,
image.Height,
GraphicsUnit.Pixel,
imgAtb);
}
// this function will grab the background image to the control it self
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
Graphics g = e.Graphics;
if (Parent != null)
{
BackColor = Color.Transparent;
int index = Parent.Controls.GetChildIndex(this);
for (int i = Parent.Controls.Count - 1; i > index; i--)
{
Control c = Parent.Controls[i];
if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
{
Bitmap bmp = new Bitmap(c.Width, c.Height, g);
c.DrawToBitmap(bmp, c.ClientRectangle);
g.TranslateTransform(c.Left - Left, c.Top - Top);
g.DrawImageUnscaled(bmp, Point.Empty);
g.TranslateTransform(Left - c.Left, Top - c.Top);
bmp.Dispose();
}
}
}
else
{
g.Clear(Parent.BackColor);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, Color.Transparent)), this.ClientRectangle);
}
}
// simple drawing circle function
public void DrawCircles()
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(Color.Green, 3))
{
this.graphics.DrawEllipse(p, 25, 25, 20, 20);
}
}
}
// simple drawing rectable function
public void DrawRectangle()
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(Color.Red, 3))
{
this.graphics.DrawRectangle(p, 50, 50, 40, 40);
}
}
}
// Layer control image property
public Image Image
{
get
{
return image;
}
set
{
image = value;
// this will make the control to be redrawn
this.Invalidate();
}
}
}
Example how to use it:
LayerControl lc = new LayerControl(100, 100);
lc.Location = new Point(0, 0);
lc.DrawRectangle();
LayerControl lc2 = new LayerControl(100, 100);
lc2.Location = new Point(0, 0);
lc2.DrawCircles();
LayerControl lc3 = new LayerControl(100, 100);
lc3.Location = new Point(0, 0);
lc3.Image = new Bitmap(#"<Image Path>");
// adding control
this.Controls.Add(dc);
this.Controls.Add(dc2);
this.Controls.Add(dc3);
With this method you can have multiple layers that can put overlapping each other (due to the transparency feature it has).
If you want to add it in top of your PictureBox make sure to re-order the control. The Layer Control should be added before your PictureBox control.
// adding control
this.Controls.Clear();
this.Controls.Add(dc);
this.Controls.Add(dc2);
this.Controls.Add(dc3);
this.Controls.Add(PictureBox1);
Hopefully it help.
example code which working fine - take dummy image and layered the original image with custom text
public void LayerImage(System.Drawing.Image Current, int LayerOpacity)
{
Bitmap bitmap = new Bitmap(Current);
int h = bitmap.Height;
int w = bitmap.Width;
Bitmap backg = new Bitmap(w, h + 20);
Graphics g = null;
try
{
g = Graphics.FromImage(backg);
g.Clear(Color.White);
Font font = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);
RectangleF rectf = new RectangleF(70, 90, 90, 50);
Color color = Color.FromArgb(255, 128, 128, 128);
Point atpoint = new Point(backg.Width / 2, backg.Height - 10);
SolidBrush brush = new SolidBrush(color);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString("BRAND AMBASSADOR", font, brush, atpoint, sf);
g.Dispose();
MemoryStream m = new MemoryStream();
backg.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch { }
Color pixel = new Color();
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
pixel = bitmap.GetPixel(x, y);
backg.SetPixel(x, y, Color.FromArgb(LayerOpacity, pixel));
}
}
MemoryStream m1 = new MemoryStream();
backg.Save(m1, System.Drawing.Imaging.ImageFormat.Jpeg);
m1.WriteTo(Response.OutputStream);
m1.Dispose();
base.Dispose();
}
Got it working, perhaps I wasn't clear enough in my original question.
Essentially what I ended up doing was storing each layer as a separate Image object, then just hooking into the OnPaint method of my control and manually drawing the graphics in order, instead of just drawing to PictureBox.Image. Works like a charm!
The graphics capabilities of .NET drawing libraries are simple. Their main purpose is direct drawing of GUI. If you want to have layering, alpha transparency or advanced filters, then you should either use 3rd party library or roll your own drawing code.