元の画像からアスペクト比を維持したまま、ボタンコントロールの画像のサイズを変更しようとしていますので、新しい画像が潰れたり伸びたりしません。ボタン画像のアスペクト比を保持する方法
そして再び元の位置(通常のウィンドウ最大化状態)に戻すと、元の解像度が保持されます。
基本的には、すべてのモニタ解像度の画像が同じに見えるようにします。 EXのために
:(5:4)私のアプリケーションでは、グローブの 画像は、モニタの解像度1280×1024で正常に見える が、モニタの解像度を持つ楕円のように伸ばして見えます1920×1080 (16:9)
私は変換をプログラム的に行う必要があると知っていますが、それをどうやって行うのか分かりません。 OnResizeイベントで以下のメソッドを呼び出そうとしましたが、最初の反復では機能しますが、その後のサイズ変更で画像が小さくなり、最終的に画像が失われます。
public Rectangle MaintainAspectRatio(Image imgPhoto、Rectangle thumbRect) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0;私は私のようにオーバーライドのOnPaintイベントハンドラメソッドでこのコードを使用
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)thumbRect.Width/(float)sourceWidth);
nPercentH = ((float)thumbRect.Height/(float)sourceHeight);
//if we have to pad the height pad both the top and the bottom
//with the difference between the scaled height and the desired height
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((thumbRect.Width - (sourceWidth * nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = (int)((thumbRect.Height - (sourceHeight * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Rectangle retRect = new Rectangle(thumbRect.X, thumbRect.Y, destWidth, destHeight);
return retRect;
}
:オーバーライドボイドのOnPaint(PaintEventArgsのE) {base.OnPaint(e)を保護
。 グラフィックスg = e.Graphics;
int iHeight;
int iWidth;
if (Image != null)
{
Rectangle ResizedRectangle = MaintainAspectRatio(Image,ClientRectangle);
iHeight = ResizedRectangle.Height;
iWidth = ResizedRectangle.Width;
}
else
{
iWidth = ClientRectangle.Width;
iHeight = ClientRectangle.Height;
}
// Draw border
Color oLeftTopColor = SystemColors.ControlLightLight;
Color oRightBottomColor = SystemColors.ActiveCaption;
Pen oLeftTopPen = new Pen(oLeftTopColor);
Pen oRightBottomPen = new Pen(oRightBottomColor);
// top line
g.DrawLine(oLeftTopPen, 0, 0, iWidth - 1, 0);
//g.DrawLine(new Pen(SystemColors.ControlLight), 1, 1, iWidth-2, 1);
// bottom line
g.DrawLine(oRightBottomPen, 0, iHeight, iWidth - 1, iHeight);
//g.DrawLine(new Pen(SystemColors.ControlDark), 1, iHeight-2, iWidth-2, iHeight-2);
// right line
g.DrawLine(oRightBottomPen, iWidth, 0, iWidth, iHeight - 1);
//g.DrawLine(new Pen(SystemColors.ControlDark), iWidth-2, 1, iWidth-2, iHeight-2);
// left line
g.DrawLine(oLeftTopPen, 0, 0, 0, iHeight - 1);
//g.DrawLine(new Pen(SystemColors.ControlLightLight), 1, 1, 1, iHeight-2);
// Draw image
if (Image != null)
{
Rectangle oDrawRectagle = new Rectangle(
0, 0, iWidth, iHeight);
if (Enabled == false)
{
e.Graphics.DrawImage(Image, oDrawRectagle,
0, 0, Image.Width, Image.Height,
GraphicsUnit.Pixel);
}
else
{
e.Graphics.DrawImage(Image, oDrawRectagle);
}
}
// Draw text
if (Text != null)
{
int iTextTop = 5;
int iTextLeft = 5;
int iMaxTextWidth = iWidth - 10;
int iMaxTextHeight = iHeight - 10;
}
}
私はこれをどのように進めるべきですか?
簡単にする...すべてのモニター解像度で丸い形が丸く表示されるようにします。 –
これは基本的に不可能な問題です。モニターの解像度はアスペクト比を定義しません。つまり、16x9モニタの解像度は1920x1080です。したがって、4x3モニターも可能です。あなたは実際に解像度からディスプレイのアスペクト比を知ることはできません。私は専門家だとは言えませんが、xピクセル×xピクセルのイメージが対称的に見えると仮定する必要があると思います。そうでなければ、ディスプレイ上の他のものも歪んでしまいます彼らがそれを望む方法。 –