How am I doing transparent picturebox (whith image)?
2aviv20 0 Newbie Poster
Diamonddrake 397 Master Poster
Ok, first off. Welcome to daniweb. Don't make it a habit to come post ambiguous questions without even pretending you know what you are dong. Please read this before posting on daniweb. http://www.daniweb.com/forums/announcement61-2.html
With that said. this is so simple I just did it for you. Here is a snippet for a transparent Picturebox control. This one is on the house. No more freebies. You'll have to show that you are trying.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace Transparent_picturebox
{
public class TransPicturebox : Control
{
public TransPicturebox()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
this.BackColor = Color.Transparent;
}
private Image _Image;
public Image Image
{
get
{
return _Image;
}
set
{
_Image = value;
}
}
private bool _autoscale = true;
public bool AutoScale
{
get
{
return _autoscale;
}
set
{
_autoscale = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (_Image != null) //keep from crashing if there is no image
{
if (_autoscale)
{
//Auto Scale the image to fit in the text box
Rectangle rectangle = new Rectangle();
Size size = this.Image.Size;
float num = Math.Min((float)(((float)base.ClientRectangle.Width) / ((float)size.Width)), (float)(((float)base.ClientRectangle.Height) / ((float)size.Height)));
rectangle.Width = (int)(size.Width * num);
rectangle.Height = (int)(size.Height * num);
rectangle.X = (base.ClientRectangle.Width - rectangle.Width) / 2;
rectangle.Y = (base.ClientRectangle.Height - rectangle.Height) / 2;
e.Graphics.DrawImage(_Image, rectangle);
}
else
{
e.Graphics.DrawImage(_Image, new Point(0,0));
}
}
}
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.