Bala

Bala

  • NA
  • 2
  • 0

Transparent Picturebox

Sep 12 2005 9:43 AM
Hi, I have a sample project, which has a form displaying a list view and a main picture box. The list view displays a set of images and the user can drag and drop the images on the main picture box. On dropping an image to the main picture box, I am creating a sub picture box over the main picture box (the sub picture box contains the image dragged). Like this user can drag and drop as many images. I want the sub picture boxes to be transparent (i.e. if more than one sub picture boxes are there at the same location, it has to display all the images). I tried the MakeTransparent method. But it did not help. I have attached the main functions used herewith. Can somebody help me in achieving this transparency please? Thanks. Bala public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox picKDMain; private PictureBox picRT; private void Form1_Load(object sender, System.EventArgs e) { //Calling the Gallery KD_Gallery(); } private void KD_Gallery() { // Method for creating Gallery // To get items from the directory onto the ListView int i; string strExPath=Application.ExecutablePath; string strFullPath; string strFilNam; string pth=strExPath.Substring(0, strExPath.LastIndexOf("\\"))+"\\SImg\\"; DirectoryInfo dr = new DirectoryInfo(pth); FileInfo [] fs; fs = dr.GetFiles(); lvKDMain.View=View.LargeIcon; ImageList imageListLarge = new ImageList(); int imgIndex=0; for (i = 0;i<=fs.GetLength(0)-1 ;i++) { if (fs[i].Extension == ".bmp") { strFullPath=pth+fs[i].Name; int l=strFullPath.LastIndexOf(".")-strFullPath.LastIndexOf("\\"); strFilNam=strFullPath.Substring(strFullPath.LastIndexOf("\\")+1,l-1); ListViewItem item = new ListViewItem(strFilNam,imgIndex); lvKDMain.Items.Add(item); imageListLarge.Images.Add(Bitmap.FromFile(strFullPath), System.Drawing.Color.White); imageListLarge.TransparentColor = System.Drawing.Color.White; imgIndex+=1; } lvKDMain.LargeImageList = imageListLarge; } } private void KD_AddShape(bool flag,string name,int xx,int yy,int pp,int qq,string mode) { // Procedure to add an image on the Picture Box sShapeName = "BImg/" + name + sImgExtension; //Creating Runtime Picture Control at runtime picRT = new PictureBox(); picRT.Name = iShapeCount.ToString() + "_" + sShapeName; picRT.BackColor = System.Drawing.Color.Transparent; // used to get the certain position int mouseX,mouseY,picLocX,picLocY,frmX,frmY,tabX,tabY,x,y; mouseX=(int)MousePosition.X; mouseY=(int)MousePosition.Y; picLocX=(int)picKDMain.Left; picLocY=(int)picKDMain.Top; frmX=(int)this.FindForm().Left; frmY=(int)this.FindForm().Top; tabX=(int)this.tabControl1.Left; tabY=(int)this.tabControl1.Top; // x,y are the left & right position to drop the image x=mouseX-frmX-tabX-picLocX-50 ; y=mouseY-frmY-tabY-picLocY-60 ; // mode is the type of adding the image switch (mode) { case "Drag&Drop" : picRT.Location = new System.Drawing.Point(x ,y ); break; case "Open" : // Nothing to do break; case "DblClick" : picRT.Location = new System.Drawing.Point(1 ,1 ); break; case "Paste": picRT.Location = new System.Drawing.Point(xx+2 ,yy+2 ); break; } // pp,qq are the size of the picturebox // if the user wants to draw a line size is 20,20 if (picRT.Name.EndsWith("line.bmp")) picRT.Size = new System.Drawing.Size(20 ,20) ; else picRT.Size = new System.Drawing.Size(pp ,qq) ; // placing the images on the PictureBox picRT.Parent = picKDMain; picRT.SizeMode = PictureBoxSizeMode.StretchImage; picRT.Tag = 0; picKDMain.Controls.Add(picRT); picRT.BackColor = System.Drawing.Color.Transparent; KD_AddRemoveListImages(true); iShapeCount = iShapeCount + 1; string strExPath=Application.ExecutablePath; string pth=strExPath.Substring(0, strExPath.LastIndexOf(@"\bin"))+"/"; string sFile = pth + sShapeName; // type of adding is Open // the image is displayed from file if (mode == "Open") bm = (Bitmap) Bitmap.FromFile(ofdKDMain.FileName); else bm = (Bitmap) App.GetEmbeddedResource(sShapeName); bm.MakeTransparent(System.Drawing.Color.White); picRT.Image = bm; picRT.Click +=new EventHandler(picRT_Click); // Binding all the Controls again after adding new picture box AddLabels(iShapeCount,picRT); foreach(Control c in this.picKDMain.Controls) { pb.WireControl(c); } } private void picRT_Click(object sender, EventArgs e) { //Click Event on Runtime created Picture Box PictureBox picCurrent = (PictureBox) sender; sShapeName = picCurrent.Name.Substring(2); // when selected it comes front // if again selected it goes back if (val==1) { val=0; picCurrent.SendToBack(); } else { val=1; picCurrent.BringToFront(); } stbarKDMain.Panels[1].Text = "Pic Name: " + picCurrent.Name; picSelected=picCurrent; picRT = picCurrent; display(picCurrent); Bitmap bm = (Bitmap) picRT.Image; bm.MakeTransparent(System.Drawing.Color.White); picRT.Image = bm; picRT.BackColor = System.Drawing.Color.Transparent; } private void display(PictureBox pBox) { int indx; int pos=pBox.Name.IndexOf('_'); indx=int.Parse(pBox.Name.Substring(0,pos))+1; foreach (Control ctrl in groupBox1.Controls) { if (ctrl.GetType().ToString()=="System.Windows.Forms.Label") { if (ctrl.Name.ToString()==("A"+indx)) ctrl.Text="A"+ indx + " = " + pBox.Width.ToString(); if (ctrl.Name.ToString()==("B" +indx)) ctrl.Text="B" + indx + " = " + pBox.Height.ToString(); } } lblPosX.Text = "Pos X = " + pBox.Bounds.X.ToString(); lblPosY.Text = "Pos Y = " + pBox.Bounds.Y.ToString(); } }