简体   繁体   English

具有透明背景的透明图像问题

[英]Issue with transparent images with a transparent background

I'm having a problem with displaying transparent images with a transparent background. 我在显示透明背景的透明图像时遇到问题。 The transparent background takes the color of the underlying control and that is fine ... bu the problem is that some details (lines) on the underlying background are being covered the the images as can be seen in the image below. 透明背景采用底层控件的颜色,这很好......问题是底层背景上的一些细节(线条)正在覆盖图像,如下图所示。

Here is the code I am using.... This is the code for the notes.... 这是我正在使用的代码....这是笔记的代码....

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.Drawing;

namespace Simpe_Piano_new
{
    class MusicNote: PictureBox
    {
        public SoundPlayer sp = new SoundPlayer();
        Timer tmr = new Timer();
        public int pitch;        //The no. of the music key (e.g. the sound freuency).
        public int noteDuration; //Shape of note.
        public string noteShape;

        public MusicNote(int iPitch, int iNoteDuration)
            : base()
        {
            pitch = iPitch;
            noteDuration = iNoteDuration;
            Size = new Size(40, 40);
        }

        public void ShowNote()
        {   if (this.noteDuration == 1) noteShape = "Quaver.png"; 
            if (this.noteDuration == 4) noteShape = "Crotchet.png";
            if (this.noteDuration == 7) noteShape = "minim.png";
            if (this.noteDuration == 10) noteShape = "DotMin.png";
            if (this.noteDuration == 12) noteShape = "SemiBreve.png";
            this.BackgroundImage = Image.FromFile(noteShape);
            this.BackColor = Color.Transparent;
            Location = new Point((pitch * 40) - 40, 100);
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        public void PlaySound()
        {
            sp.SoundLocation = this.pitch + ".wav";
            sp.Play();
        }

        public void StopSound()
        {
            sp.SoundLocation = this.pitch + ".wav";
            sp.Stop();
        }

        public void Play()
        {
            sp.SoundLocation = this.pitch + ".wav";
            sp.Play();
            //Time to play the duration
            tmr.Interval = noteDuration;
            tmr.Start();
            tmr.Tick += new System.EventHandler(ClockTick);
        }

        void ClockTick(object sender, EventArgs e)
        {
            sp.Stop();
            tmr.Stop();
        }


    }
}

This is the code for the underlying control..the music staff 这是底层控制的代码......音乐人员

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Simpe_Piano_new
{
    public class MusicStaff: Panel
    {
        Pen myPen;
        Graphics g;

        public MusicStaff()
        {
            this.Size = new Size(1000, 150);
            this.Location = new Point(0, 0);
            this.BackColor = Color.Transparent;
            this.Paint += new PaintEventHandler(DrawLines);
        }

        private void DrawLines(object sender, PaintEventArgs pea)
        {
            myPen = new Pen(Color.Black, 1);
            g = this.CreateGraphics();
            for (int i = 1; i < 6; i++)
            {
                g.DrawLine(myPen, 0, (this.Height / 6) * i, this.Width, (this.Height / 6) * i);
            }
        }
    }
}

I have found that C# does not handle transparency really well... Any help would be greatly appreciated.. 我发现C#不能很好地处理透明度......任何帮助都会非常感激..

Two mistakes. 两个错误。 PictureBox supports transparent images well, as long as you set its BackColor property to Color.Transparent. PictureBox支持透明图像,只要将其BackColor属性设置为Color.Transparent即可。 Which you did for the MusicStaff but not for the MusicNote. 你为MusicStaff做了哪些但没有为MusicNote做过。 Layered transparency does not work, you don't need MusicStaff to be transparent, just the picture boxes. 分层透明不起作用,你不需要MusicStaff透明,只需要图片框。

This kind of transparency is simulated by asking the Parent to paint itself into the control to provide the background pixels. 通过要求Parent将自身绘制到控件中以提供背景像素来模拟这种透明度。 Which is your second mistake, you use CreateGraphics() in your DrawLines() method. 这是你的第二个错误,你在DrawLines()方法中使用CreateGraphics()。 Which draws directly to the screen, not the control surface. 它直接绘制到屏幕上,而不是控制界面。 You must use pea.Graphics here. 你必须在这里使用pea.Graphics。

Do note that the value-add you get from using PictureBox is a very low one. 请注意,使用PictureBox获得的增值是非常低的。 Controls are expensive and you'll easily burn up hundreds of them to display a sheet of music. 控件很昂贵,你可以很容易地烧掉数百个来显示一张音乐。 You'll notice, it will become slow to paint itself. 你会注意到,涂漆本身会很慢。 You avoid this by having MusicStaff just paint the notes itself, using Graphics.DrawImage() gets the job done. 你可以通过让MusicStaff只是自己绘制笔记来避免这种情况,使用Graphics.DrawImage()来完成工作。 Transparency effects are now much simpler as well, just layers of paint. 透明效果现在也变得更加简单,只是油漆层。 Which is the way WPF does it. 这是WPF的方式。 The only inconvenience you'll have to deal with is that mouse hit testing isn't as simple anymore, you need to map the panel's MouseDown event's coordinates to a note. 您需要处理的唯一不便是鼠标命中测试不再那么简单,您需要将面板的MouseDown事件坐标映射到注释。 Just keep a List that keeps track where every note is displayed. 只需保留一个列表,跟踪每个音符的显示位置。 You'll use that for painting as well as mouse hit testing. 您将使用它进行绘画以及鼠标命中测试。

add the top control "MusicNote" in the children of the underlying control "MusicStaff" 在底层控件“Mus​​icStaff”的子级中添加顶级控件“Mus​​icNote”

something like that after -Initializing all components- 之后的事情 - 初始化所有组件 -

// mStaff: the MusicStaff object
// mNote: the MusicNote object
mStaff.Children.Add(mNote);

in old scenario, the form is the parent of both of them, so they display the form background in any transparent area 在旧方案中,表单是两者的父级,因此它们在任何透明区域中显示表单背景

after modifying the parent of the "MusicNote", it displays the "MusicStaff" background in the transparent area 修改“MusicNote”的父级后,它会在透明区域中显示“MusicStaff”背景

I hope that help! 我希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM