简体   繁体   English

按钮图像的两面被裁剪

[英]Button image sides cropped

I'm trying to fit image to button perfectly. 我正在尝试使图像完美地适合按钮。 But the image is cropped on its right and bottom faces, see attached print screen: 但是图像会在其右侧和底部被裁剪,请参见所附的打印屏幕: 在此处输入图片说明

I edited the button as follows: 我对按钮进行了如下编辑:

var l_oStopImage = Image.FromFile(@"C:\Users\AmitL\Downloads\Button-2-stop-icon72p.png");
var l_oStopPic = new Bitmap(l_oStopImage , new Size(btnStopOperation.Width, btnStopOperation.Height));
btnStopOperation.Image = l_oStopPic ;
btnStopOperation.ImageAlign = System.Drawing.ContentAlignment.MiddleCenter;

btnStopOperation.TabStop = false;
btnStopOperation.FlatStyle = FlatStyle.Flat;
btnStopOperation.FlatAppearance.BorderSize = 0;

I also tried to edit the BackgroundImageLayout but none of the ImageLayouts fixed the problem.. 我也尝试编辑BackgroundImageLayout但是没有一个ImageLayouts解决此问题。

Any suggestions? 有什么建议么? Thanks in advance 提前致谢

背景图

1 https://msdn.microsoft.com/en-us/library/system.windows.forms.imagelayout(v=vs.110).aspx 1 https://msdn.microsoft.com/zh-CN/library/system.windows.forms.imagelayout(v=vs.110).aspx

You should use stretch, I suggest in designtime (this is not java where you have to add elements by code): 我建议在设计时使用拉伸(这不是Java,必须通过代码添加元素):

            this.buttonOk.BackColor = System.Drawing.SystemColors.MenuHighlight;
            this.buttonOk.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonOk.BackgroundImage")));
            this.buttonOk.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.buttonOk.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.buttonOk.Location = new System.Drawing.Point(475, 15);
            this.buttonOk.Name = "buttonOk";
            this.buttonOk.Size = new System.Drawing.Size(50, 50);
            this.buttonOk.TabIndex = 11;
            this.buttonOk.UseVisualStyleBackColor = false;
            this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click);

And it will work, done it many times before 它会工作,之前已经做了很多次

I got this code from my own working Form1.Designer.cs but because of that: please use the Visual Studio designer and don't try to write all this code / logic in your constructor or something. 我从我自己的工作Form1.Designer.cs中获得了此代码,但是由于这个原因:请使用Visual Studio设计器,不要尝试在构造函数中或其他内容中编写所有这些代码/逻辑。

The problem is because you are showing an image with the same size as your button. 问题是因为您正在显示与按钮大小相同的图像。

When you want an image fit in your button, the width and height of image should be at least 1 point less than your button size. 当您希望图像适合按钮时,图像的宽度和高度至少应比按钮尺寸小1点。 (or in other word, you can set your button width and height 1 point more than the image size). (或换句话说,您可以将按钮的宽度和高度设置为比图像大小大1点)。

So you can change your code to this: 因此,您可以将代码更改为此:

var l_oStopPic = new Bitmap(l_oStopImage ,
    new Size(btnStopOperation.Width-1, btnStopOperation.Height-1));

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

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