透明な画像とC#のサポート
-
23-08-2019 - |
質問
また、Windows FormsプVS2008年としたい表示画像上の他のイメージは、gifまたはものを透明パーツです。
基本的にはしていま大きな画像では少し上にイメージの場合で、そのちょっとして登場し、一つの画像はユーザーが行います。
って活動に活用しようとしているpictureboxが、このように見えません、ご意見募集
解決
私は数日前に似たような状況にありました。あなたのイメージをホストする透明コントロールを作成することができます。
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層を有しています。
その後、PictureBox
イベントにハンドラを追加し、OnPaint
オーバーロードのいずれかを使用し、e.Graphics.DrawImage()
に大きな/下の画像を置きます。あなたはImage.FromFile()
を使用して画像を読み込むことができます。
小/トップ画像はアルファチャネルを有し、作業にオーバーレイの背景に透明でなければなりません。あなたは、Photoshopや似たようで、かなり簡単にこれを確実にすることができるはずです。あなたは、このようなPNGなどのアルファチャンネルをサポートする形式で保存していることを確認します。
vb.netコード(レオン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およびサポート(その他の投稿、下記複数の繰り返コンポーネントのラインナップを解決すこともで)
- をWinform
- をx2pictureBoxes
- foreground_pictureBox//写真ボックス"のフロント'の'background'
- background_pictureBox//写真ボックス'裏'を'foreground'
- 追加の塗装"イベントのための各pictureBox
- 選択オブジェクトのデザイナー'
- を選択してプロパティタブまたは右クリックしてからお選びのポップアップメニュー)
- を選択し、ボタンイベント(小型電撃)
- ダブルクリックし、空欄の右の"塗装"のイベント
- 次のコードの形式の'load'関数れていない場合の追加、利用のアプローチステップ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':
=
private void background_pictureBox_Paint(object sender, PaintEventArgs e)
{
...foreground_pictureBox_Paint(sender, e);
}
6.内のforeground_pictureBox_Paintのコ追加もグラフィック電話に表示したいへ
このトピックを繰り返しそのものについ:
私はいつも、私は単一のPictureBoxやコントロールを使用して、合成画像に自分自身を持っていたことがわかりました。透明パーツで2 pictureboxesを持つことは、私のために働いたことはない。