24

私は VS 2008 で Windows フォーム アプリケーションに取り組んでおり、ある画像を別の画像の上に表示したいと考えています。一番上の画像は gif または透明部分のあるものです。

基本的に私は大きな画像を持っています。その上に小さな画像を置きたいので、ユーザーには 1 つの画像として表示されます。

ピクチャボックスを使用しようとしていますが、うまくいかないようです。何か提案はありますか?

4

6 に答える 6

27

私は数日前に同じような状況にありました。画像をホストするための透過的なコントロールを作成できます。

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();
        }
    }
}
于 2009-01-12T07:26:44.943 に答える
6

PictureBox には 2 つのイメージ レイヤーがあります: BackgroundImage と Image は、描画とクリアを含めて互いに独立して使用できます。

于 2011-10-14T12:17:35.307 に答える
4

大きな/下の画像を に配置し、イベントにPictureBoxハンドラーを追加して、オーバーロードの 1 つを使用します。を使用して画像を読み込むことができます。OnPainte.Graphics.DrawImage()Image.FromFile()

オーバーレイを機能させるには、小さい/上の画像にアルファ チャネルがあり、背景が透明である必要があります。これは、Photoshop などで簡単に確認できるはずです。PNG などのアルファ チャネルをサポートする形式で保存してください。

于 2008-12-27T19:09:35.160 に答える
3

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
于 2012-08-20T17:32:14.337 に答える
2

同様の投稿のリストは、この返信の最後に記載されています。

この返信は、pictureBoxes と Winforms に対応しています (以下の他の投稿で、WPF が既にこれをうまく解決していることを繰り返し述べています)。

  1. Winフォームを作成
  2. x2 の pictureBox を作成する
    • foreground_pictureBox // 「背景」の「前」にある画像ボックス
    • background_pictureBox // 「前景」の「後ろ」にある画像ボックス
  3. 各 pictureBox に「ペイント」イベントを追加します
    • 「デザイナー」でオブジェクトを選択
    • 「プロパティ」タブを選択します (または、右クリックしてポップアップ メニューから選択します)。
    • イベント ボタン (小さな稲妻) を選択します。
    • 「ペイント」イベントの右側にある空のフィールドをダブルクリックします
  4. 次のコードをメイン フォームの「ロード」関数に追加します (まだ追加されていない場合は、手順 3 のアプローチを使用して、「ペイント」ではなく「ロード時」を選択します)。

=

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」呼び出し内で、前景に表示したいグラフィック呼び出しを追加します。

このトピックは、いくつかの投稿で繰り返されているようです。

作り方-picturebox-透明

c-sharp-picturebox-transparent-background-doesnt-to-work

make-overlapping-picturebox-transparent-in-c-net

ピクチャーボックスの問題

于 2014-02-26T15:32:40.643 に答える
0

単一のピクチャボックスまたはコントロールを使用して、自分で画像を合成する必要があることにいつも気付きました。透明な部分を持つ2つのピクチャボックスを持つことは、私にとってはうまくいきませんでした.

于 2008-12-28T20:21:43.860 に答える