Here is some code that uses
SetStyle, it's for a custom button but maybe it can be adapted for a PictureBox:
namespace PushButtonTransparent
{
using System.Drawing;
using System.Windows.Forms;
using System;
public class PushButtonTransparent : Control
{
private bool pressed;
public PushButtonTransparent()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
public Image ImagePressed { get; set; }
public Image ImageNotPressed { get; set; }
public Point TextPosition { get; set; }
public StringAlignment TextAlignment { get; set; }
public Color ForeColorPressed { get; set; }
public bool Pressed
{
get
{
return this.pressed;
}
set
{
this.pressed = value;
this.Invalidate();
}
}
protected override CreateParams CreateParams
{
get
{
const int WsExTransparent = 0x20;
var cp = base.CreateParams;
cp.ExStyle |= WsExTransparent;
return cp;
}
}
protected override void OnPaint(PaintEventArgs pevent)
{
Image buttonImage;
var g = pevent.Graphics;
int width;
int height;
SolidBrush textBrush;
if (this.pressed)
{
buttonImage = this.ImagePressed;
}
else
{
buttonImage = this.ImageNotPressed;
}
if (buttonImage == null)
{
width = Convert.ToInt32(this.ClientRectangle.Width * scaleFactor);
height = Convert.ToInt32(this.ClientRectangle.Height * scaleFactor);
g.DrawRectangle(Pens.Black, 0, 0, width - 1, height - 1);
}
else
{
width = Convert.ToInt32(buttonImage.Size.Width * scaleFactor);
height = Convert.ToInt32(buttonImage.Size.Height * scaleFactor);
var rect = new Rectangle(0, 0, width, height);
g.DrawImage(buttonImage, rect);
}
if (string.IsNullOrEmpty(this.Text))
{
return;
}
if (this.pressed && !this.ForeColorPressed.IsEmpty)
{
textBrush = new SolidBrush(this.ForeColorPressed);
}
else
{
textBrush = new SolidBrush(this.ForeColor);
}
if (!this.TextPosition.IsEmpty)
{
float x = this.TextPosition.X * scaleFactor;
float y = this.TextPosition.Y * scaleFactor;
g.DrawString(this.Text, this.Font, textBrush, x, y);
}
else
{
width = Convert.ToInt32(this.Width * scaleFactor);
height = Convert.ToInt32(this.Height * scaleFactor);
var rectF = new RectangleF(0F, (height / 2) - (this.Font.Height / 2), width, height);
var strFormat = new StringFormat();
strFormat.Alignment = this.TextAlignment;
g.DrawString(this.Text, this.Font, textBrush, rectF, strFormat);
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
}
}