具有不同背景颜色的两个控件上的透明图像 [英] Transparent image over two controls with different back colors

查看:33
本文介绍了具有不同背景颜色的两个控件上的透明图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将透明图像放置在具有不同背景颜色的两个相邻控件上.
我希望图像保持透明,这意味着图像需要显示每个控件的背景色.

I am trying to place a transparent image over two adjacent controls that have different background colors.
I want the image to remain transparent, meaning the Image needs to show the backcolor of each control.

控件是设置为不同背景颜色的两个面板,图像(PictureBox 或其他)放置在两个面板控件之间.

The controls are two Panels set to different background colors and the Image (PictureBox or otherwise) is placed between the two panel controls.

Public Class frmMain 
    Private Img1 As Image = Image.FromFile("C:\xxxx.png") 

    Private Sub frmMain_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint 
        e.Graphics.DrawImage(Img1, 5, 5) 
    End Sub 
End Class

推荐答案

让我们试试这个.

  • 在项目中创建一个新类,将其命名为 TPanel 并粘贴您可以在下面找到的自定义 Panel 类,覆盖现有定义.
  • 编译项​​目,然后在 ToolBox 中找到新的 TPanel 控件并将一个实例放入一个 Form 中.
    在窗体上,而不是在其中一个彩色面板内,否则它将成为另一个控件的子控件,并将被限制在其边界内.
  • TPanel的Paint事件添加一个事件处理程序,并将此代码插入到处理程序方法中:
  • Create a new class in the Project, call it TPanel and paste in the custom Panel class you can find below, overwriting the existing definition.
  • Compile the Project then find the new TPanel control in the ToolBox and drop one instance inside a Form.
    On the Form, not inside one of the Colored Panels, otherwise it will become child of another control and it will be confined inside its bounds.
  • Add an event handler to the Paint event of the TPanel and insert this code inside the handler method:
Private Sub TPanel1_Paint(sender As Object, e As PaintEventArgs) Handles TPanel1.Paint
    Dim canvas As Control = DirectCast(sender, Control)
    Dim rect As Rectangle = ScaleImageFrame(imgBasketBall, canvas.ClientRectangle)

    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
    e.Graphics.CompositingMode = CompositingMode.SourceOver
    e.Graphics.PixelOffsetMode = PixelOffsetMode.Half
    e.Graphics.DrawImage(imgBasketBall, rect)
End Sub

Private Function ScaleImageFrame(sourceImage As Bitmap, destinationFrame As Rectangle) As Rectangle
    Dim rect As RectangleF = New RectangleF(0, 0, sourceImage.Width, sourceImage.Height)
    'Define the ratio between the Image Rectangle and the Container ClientRectangle
    Dim ratio As Single = CType(Math.Max(destinationFrame.Width, destinationFrame.Height) /
                                Math.Max(rect.Width, rect.Height), Single)
    rect.Size = New SizeF(rect.Width * ratio, rect.Height * ratio)
    'Use Integer division to avoid negative values
    rect.Location = New Point((destinationFrame.Width - CInt(rect.Width)) \ 2,
                              (destinationFrame.Height - CInt(rect.Height)) \ 2)
    Return Rectangle.Round(rect)
End Function

  • 在表单中,创建一个包含图像的位图对象的实例;还要设置面板​​的位置 (TPanel)
    名为 panColored1panColored2 的控件应该是图像必须位于的两个现有面板的名称定位.示例代码使用 TPanel1.Location( (...) )
  • 将图像定位在 2 个面板的中间

    • In the Form, create an instance of a Bitmap object that will contain the Image; also set the Location of the Panel (TPanel)
      The Controls called panColored1 and panColored2 are supposed to be the names of the two existing Panels where the Image must be positioned. The sample code positions the Image in the middle of the 2 Panels, using TPanel1.Location( (...) )
    • Private imgBasketBall As Bitmap = Nothing
      
      Public Sub New()
          InitializeComponent()
          imgBasketBall = DirectCast(Image.FromFile("basketball.png").Clone(), Bitmap)
          TPanel1.Size = New Size(120, 120)
          TPanel1.Location = New Point(panColored1.Left + (panColored1.Width - TPanel1.Width) \ 2,
                                       panColored1.Top + (panColored1.Height + panColored2.Height - TPanel1.Height) \ 2)
          TPanel1.BringToFront()
      End Sub
      

      结果:

           Bitmap Size            Bitmap Size 
           (1245x1242)            (1178x2000)
      

      TPanel(透明面板)类:

      The TPanel (Transparent Panel) class:

      Imports System.ComponentModel
      
      <DesignerCategory("Code")>
      Public Class TPanel
          Inherits Panel
          Private Const WS_EX_TRANSPARENT As Integer = &H20
          Public Sub New()
              Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or
                          ControlStyles.UserPaint Or
                          ControlStyles.Opaque Or
                          ControlStyles.ResizeRedraw, True)
              Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
              Me.UpdateStyles()
          End Sub
      
          Protected Overrides Sub OnPaint(e As PaintEventArgs)
              e.Graphics.FillRectangle(Brushes.Transparent, Me.ClientRectangle)
              MyBase.OnPaint(e)
          End Sub
      
          Protected Overrides ReadOnly Property CreateParams() As CreateParams
              Get
                  Dim parameters As CreateParams = MyBase.CreateParams
                  parameters.ExStyle = parameters.ExStyle Or WS_EX_TRANSPARENT
                  Return parameters
              End Get
          End Property
      End Class
      

      这篇关于具有不同背景颜色的两个控件上的透明图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆