私は VS 2008 で Windows フォーム アプリケーションに取り組んでおり、ある画像を別の画像の上に表示したいと考えています。一番上の画像は gif または透明部分のあるものです。
基本的に私は大きな画像を持っています。その上に小さな画像を置きたいので、ユーザーには 1 つの画像として表示されます。
ピクチャボックスを使用しようとしていますが、うまくいかないようです。何か提案はありますか?
私は VS 2008 で Windows フォーム アプリケーションに取り組んでおり、ある画像を別の画像の上に表示したいと考えています。一番上の画像は gif または透明部分のあるものです。
基本的に私は大きな画像を持っています。その上に小さな画像を置きたいので、ユーザーには 1 つの画像として表示されます。
ピクチャボックスを使用しようとしていますが、うまくいかないようです。何か提案はありますか?
私は数日前に同じような状況にありました。画像をホストするための透過的なコントロールを作成できます。
using System;
using System.Windows.Forms;
using System.Drawing;
public class TransparentControl : Control
{
private readonly Timer refresher;
private Image _image;
public TransparentControl()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
refresher = new Timer();
refresher.Tick += TimerOnTick;
refresher.Interval = 50;
refresher.Enabled = true;
refresher.Start();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnMove(EventArgs e)
{
RecreateHandle();
}
protected override void OnPaint(PaintEventArgs e)
{
if (_image != null)
{
e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//Do not paint background
}
//Hack
public void Redraw()
{
RecreateHandle();
}
private void TimerOnTick(object source, EventArgs e)
{
RecreateHandle();
refresher.Stop();
}
public Image Image
{
get
{
return _image;
}
set
{
_image = value;
RecreateHandle();
}
}
}
PictureBox には 2 つのイメージ レイヤーがあります: BackgroundImage と Image は、描画とクリアを含めて互いに独立して使用できます。
大きな/下の画像を に配置し、イベントにPictureBox
ハンドラーを追加して、オーバーロードの 1 つを使用します。を使用して画像を読み込むことができます。OnPaint
e.Graphics.DrawImage()
Image.FromFile()
オーバーレイを機能させるには、小さい/上の画像にアルファ チャネルがあり、背景が透明である必要があります。これは、Photoshop などで簡単に確認できるはずです。PNG などのアルファ チャネルをサポートする形式で保存してください。
vb.net コード (Leon Tayson のすべてのクレジット):
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class TransparentControl
Inherits Control
Private ReadOnly Local_Timer As Timer
Private Local_Image As Image
Public Sub New()
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
BackColor = Color.Transparent
Local_Timer = New Timer
With Local_Timer
.Interval = 50
.Enabled = True
.Start()
End With
AddHandler Local_Timer.Tick, AddressOf TimerOnClick
End Sub
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams
cp = MyBase.CreateParams
cp.ExStyle = &H20
Return cp
End Get
End Property
Protected Overrides Sub OnMove(ByVal e As System.EventArgs)
MyBase.OnMove(e)
RecreateHandle()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
If Local_Image IsNot Nothing Then _
e.Graphics.DrawImage(Local_Image, New Rectangle(0, 0, (Width / 2) - (Local_Image.Width / 2), (Height / 2) - (Local_Image.Height / 2)))
End Sub
Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
' DO NOT PAINT BACKGROUND
End Sub
''' <summary>
''' Hack
''' </summary>
''' <remarks></remarks>
Public Sub ReDraw()
RecreateHandle()
End Sub
Private Sub TimerOnClick(ByVal sender As Object, ByVal e As System.EventArgs)
RecreateHandle()
Local_Timer.Stop()
End Sub
Public Property Image As Image
Get
Return Local_Image
End Get
Set(ByVal value As Image)
Local_Image = value
RecreateHandle()
End Set
End Property
End Class
同様の投稿のリストは、この返信の最後に記載されています。
この返信は、pictureBoxes と Winforms に対応しています (以下の他の投稿で、WPF が既にこれをうまく解決していることを繰り返し述べています)。
=
private void cFeedback_Form_Load(object sender, EventArgs e)
{
...
// Ensure that it is setup with transparent background
foreground_pictureBox.BackColor = Color.Transparent;
// Assign it's 'background'
foreground_pictureBox.Parent = background_pictureBox;
...
}
5. 「background_pictureBox」の「paint」呼び出しでは、次のようになります。
=
private void background_pictureBox_Paint(object sender, PaintEventArgs e)
{
...foreground_pictureBox_Paint(sender, e);
}
6 . 「foreground_pictureBox_Paint」呼び出し内で、前景に表示したいグラフィック呼び出しを追加します。
このトピックは、いくつかの投稿で繰り返されているようです。
c-sharp-picturebox-transparent-background-doesnt-to-work
単一のピクチャボックスまたはコントロールを使用して、自分で画像を合成する必要があることにいつも気付きました。透明な部分を持つ2つのピクチャボックスを持つことは、私にとってはうまくいきませんでした.