简体   繁体   English

在C#中编辑按钮的背景

[英]Edit the background of a button in c#

I have a little problem of mine to solve. 我有一个小问题要解决。 I have a button which has an image background.I tried to color the whole button but the image can not be seen after coloring the whole button. 我有一个具有图像背景的按钮。我试图给整个按钮上色,但是在给整个按钮上色后看不到图像。 How can I edit this "imagebutton" like in this example? 如何像本例中一样编辑此“图像按钮”? http://i.stack.imgur.com/XaQQQ.png http://i.stack.imgur.com/XaQQQ.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bura
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent(); 
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.BackgroundImage != null)
            {
                button2.BackgroundImage = null;
                button2.BackColor = Color.Black; 
            }

            else {
                button2.BackgroundImageLayout = ImageLayout.Stretch; 
                button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            button2.BackgroundImageLayout = ImageLayout.Stretch;
            button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
        }
    }
}

This 这个

Just made a button by using the designer with the following code: 通过使用以下代码的设计器,刚完成一个按钮:

this.button1.BackColor = System.Drawing.Color.DodgerBlue;
this.button1.BackgroundImage = global::WindowsFormsApplication.Properties.Resources.ChargeImage;
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.button1.Image = global::WindowsFormsApplication.Properties.Resources.DatabaseImage;
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(264, 160);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = false;

And that is the result: 结果是:

带有图像的按钮的屏幕截图

So what is your problem? 那你怎么了

You can edit your pictures with a method like this: 您可以使用以下方法编辑图片:

private static void DrawLinesOnBitmap(Bitmap bmp)
{
   using (var p = new Pen(Color.Black, 5))
   {
      using (var g = Graphics.FromImage(bmp))
      {
         g.DrawLine(p, 0, 0, bmp.Width, bmp.Height);
      }
   }
}

This method adds a line from the left top corner to the right bottom corner. 此方法从左上角到右下角添加一条线。 Just draw some more lines and you should get your wanted result. 只需画一些线,就可以得到想要的结果。

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

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