c# winforms의 투명한 이미지
-
23-08-2019 - |
문제
2008 대 VS의 Windows Forms 응용 프로그램에서 작업 중이며, 하나의 이미지를 다른 이미지 위에 표시하고 상단 이미지는 GIF 또는 투명한 부분을 가진 무언가입니다.
사진 상자를 사용하려고했지만 이것은 효과가 없었던 것 같습니다. 어떤 제안도?
해결책
나는 며칠 전에 비슷한 상황에 처해있었습니다. 이미지를 호스팅하기 위해 투명한 컨트롤을 만들 수 있습니다.
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();
}
}
}
다른 팁
큰/하단 이미지를 a PictureBox
, 그런 다음 핸들러를 추가하십시오 OnPaint
이벤트 및 사용 중 하나 e.Graphics.DrawImage()
과부하. 사용을 사용하여 이미지를로드 할 수 있습니다 Image.FromFile()
.
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
유사한 게시물 목록은이 답변의 맨 아래에 참조됩니다.
이 답변은 PictureBox 및 WinForms를 다룹니다 (아래의 다른 게시물에서는 WPF가 이미 잘 해결된다는 것을 반복합니다)
- Winform을 만듭니다
- X2 PictureBox를 만듭니다
- 포 그라운드_picturebox // '배경'의 앞쪽 '사진 상자'
- background_picturebox // '포 그라운드'뒤에 '사진 상자'
- 각 PictureBox의 '페인트'이벤트를 추가하십시오
- '디자이너'에서 개체를 선택하십시오.
- 이벤트 버튼 (작은 번개 볼트)을 선택하십시오.
- 빈 필드에서 '페인트'이벤트 오른쪽으로 두 번 클릭하십시오.
- 다음 코드를 기본 양식의 '로드'함수에 추가하십시오 (아직 추가되지 않은 경우 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. 'Paint'에서 'background_picturebox'를 호출합니다.
=
private void background_pictureBox_Paint(object sender, PaintEventArgs e)
{
...foreground_pictureBox_Paint(sender, e);
}
6. '포 그라운드_picturebox_paint'호출 내에서 전경에 표시하려는 그래픽 호출을 추가하십시오.
이 주제는 여러 게시물로 반복됩니다.
나는 항상 단일 사진 상자 나 컨트롤을 사용하여 이미지를 직접 합성해야한다는 것을 알았습니다. 투명한 부품을 가진 두 개의 그림 박스를 갖는 것은 결코 저에게 효과가 없었습니다.
제휴하지 않습니다 StackOverflow