DoboWiki
Top
> .NETプログラミング研究/113 をテンプレートにして作成
.NETプログラミング研究/113 をテンプレートにして作成
開始行:
#title(文字列を描画したときの大きさを正確に計測する)
#navi(.NETプログラミング研究)
#contents
*文字列を描画したときの大きさを正確に計測する [#x9b591fe]
**.NET Frameworkで用意されている3つの方法 [#f8c110bc]
Graphics.DrawStringメソッドやTextRenderer.DrawTextメソッ...
Graphics.MeasureStringメソッドを呼び出す時にStringFormat....
**実際に描画することで、正確に計測する [#fc0ee2f2]
実際に描画される大きさを正確に計測するには、やはり実際に...
これを参考にさせていただいて、私も以下のようなコードを書...
**コード [#f43ce5e5]
#code(vbnet){{
'Imports System.Drawing
''' <summary>
''' Graphics.DrawStringで文字列を描画した時の大きさと位置...
''' </summary>
''' <param name="g">文字列を描画するGraphics</param>
''' <param name="text">描画する文字列</param>
''' <param name="font">描画に使用するフォント</param>
''' <param name="proposedSize">これ以上大きいことはないと...
''' できるだけ小さくすること。</param>
''' <param name="stringFormat">描画に使用するStringFormat...
''' <returns>文字列が描画される範囲。
''' 見つからなかった時は、Rectangle.Empty。</returns>
Public Shared Function MeasureStringPrecisely(g As Graphi...
text As String, font As Font, proposedSize As Siz...
stringFormat As StringFormat) As Rectangle
'解像度を引き継いで、Bitmapを作成する
Dim bmp As New Bitmap(proposedSize.Width, proposedSiz...
'BitmapのGraphicsを作成する
Dim bmpGraphics As Graphics = Graphics.FromImage(bmp)
'Graphicsのプロパティを引き継ぐ
bmpGraphics.TextRenderingHint = g.TextRenderingHint
bmpGraphics.TextContrast = g.TextContrast
bmpGraphics.PixelOffsetMode = g.PixelOffsetMode
'文字列の描かれていない部分の色を取得する
Dim backColor As Color = bmp.GetPixel(0, 0)
'実際にBitmapに文字列を描画する
bmpGraphics.DrawString(text, font, Brushes.Black, _
New RectangleF(0.0F, 0.0F, proposedSize.Width, pr...
stringFormat)
bmpGraphics.Dispose()
'文字列が描画されている範囲を計測する
Dim resultRect As Rectangle = MeasureForegroundArea(b...
bmp.Dispose()
Return resultRect
End Function
''' <summary>
''' 指定されたBitmapで、backColor以外の色が使われている範...
''' </summary>
Private Shared Function MeasureForegroundArea(bmp As Bitm...
backColor As Color) As Rectangle
Dim backColorArgb As Integer = backColor.ToArgb()
Dim maxWidth As Integer = bmp.Width
Dim maxHeight As Integer = bmp.Height
Dim x As Integer
Dim y As Integer
'左側の空白部分を計測する
Dim leftPosition As Integer = -1
For x = 0 To maxWidth - 1
For y = 0 To maxHeight - 1
'違う色を見つけたときは、位置を決定する
If bmp.GetPixel(x, y).ToArgb() <> backColorAr...
leftPosition = x
Exit For
End If
Next
If 0 <= leftPosition Then
Exit For
End If
Next
'違う色が見つからなかった時
If leftPosition < 0 Then
Return Rectangle.Empty
End If
'右側の空白部分を計測する
Dim rightPosition As Integer = -1
For x = maxWidth - 1 To leftPosition + 1 Step -1
For y = 0 To maxHeight - 1
If bmp.GetPixel(x, y).ToArgb() <> backColorAr...
rightPosition = x
Exit For
End If
Next
If 0 <= rightPosition Then
Exit For
End If
Next
If rightPosition < 0 Then
rightPosition = leftPosition
End If
'上の空白部分を計測する
Dim topPosition As Integer = -1
For y = 0 To maxHeight - 1
For x = leftPosition To rightPosition
If bmp.GetPixel(x, y).ToArgb() <> backColorAr...
topPosition = y
Exit For
End If
Next
If 0 <= topPosition Then
Exit For
End If
Next
If topPosition < 0 Then
Return Rectangle.Empty
End If
'下の空白部分を計測する
Dim bottomPosition As Integer = -1
For y = maxHeight - 1 To topPosition + 1 Step -1
For x = leftPosition To rightPosition
If bmp.GetPixel(x, y).ToArgb() <> backColorAr...
bottomPosition = y
Exit For
End If
Next
If 0 <= bottomPosition Then
Exit For
End If
y -= 1
Next
If bottomPosition < 0 Then
bottomPosition = topPosition
End If
'結果を返す
Return New Rectangle(leftPosition, topPosition, _
rightPosition - leftPosition, bottomPosition - to...
End Function
Private Shared Function MeasureForegroundArea(bmp As Bitm...
Return MeasureForegroundArea(bmp, bmp.GetPixel(0, 0))
End Function
}}
#code(csharp){{
//using System.Drawing;
/// <summary>
/// Graphics.DrawStringで文字列を描画した時の大きさと位置...
/// </summary>
/// <param name="g">文字列を描画するGraphics</param>
/// <param name="text">描画する文字列</param>
/// <param name="font">描画に使用するフォント</param>
/// <param name="proposedSize">これ以上大きいことはないと...
/// できるだけ小さくすること。</param>
/// <param name="stringFormat">描画に使用するStringFormat...
/// <returns>文字列が描画される範囲。
/// 見つからなかった時は、Rectangle.Empty。</returns>
public static Rectangle MeasureStringPrecisely(Graphics g,
string text, Font font, Size proposedSize, StringForm...
{
//解像度を引き継いで、Bitmapを作成する
Bitmap bmp = new Bitmap(proposedSize.Width, proposedS...
//BitmapのGraphicsを作成する
Graphics bmpGraphics = Graphics.FromImage(bmp);
//Graphicsのプロパティを引き継ぐ
bmpGraphics.TextRenderingHint = g.TextRenderingHint;
bmpGraphics.TextContrast = g.TextContrast;
bmpGraphics.PixelOffsetMode = g.PixelOffsetMode;
//文字列の描かれていない部分の色を取得する
Color backColor = bmp.GetPixel(0, 0);
//実際にBitmapに文字列を描画する
bmpGraphics.DrawString(text, font, Brushes.Black,
new RectangleF(0f, 0f, proposedSize.Width, propos...
stringFormat);
bmpGraphics.Dispose();
//文字列が描画されている範囲を計測する
Rectangle resultRect = MeasureForegroundArea(bmp, bac...
bmp.Dispose();
return resultRect;
}
/// <summary>
/// 指定されたBitmapで、backColor以外の色が使われている範...
/// </summary>
private static Rectangle MeasureForegroundArea(Bitmap bmp...
{
int backColorArgb = backColor.ToArgb();
int maxWidth = bmp.Width;
int maxHeight = bmp.Height;
//左側の空白部分を計測する
int leftPosition = -1;
for (int x = 0; x < maxWidth; x++)
{
for (int y = 0; y < maxHeight; y++)
{
//違う色を見つけたときは、位置を決定する
if (bmp.GetPixel(x, y).ToArgb() != backColorA...
{
leftPosition = x;
break;
}
}
if (0 <= leftPosition)
{
break;
}
}
//違う色が見つからなかった時
if (leftPosition < 0)
{
return Rectangle.Empty;
}
//右側の空白部分を計測する
int rightPosition = -1;
for (int x = maxWidth - 1; leftPosition < x; x--)
{
for (int y = 0; y < maxHeight; y++)
{
if (bmp.GetPixel(x, y).ToArgb() != backColorA...
{
rightPosition = x;
break;
}
}
if (0 <= rightPosition)
{
break;
}
}
if (rightPosition < 0)
{
rightPosition = leftPosition;
}
//上の空白部分を計測する
int topPosition = -1;
for (int y = 0; y < maxHeight; y++)
{
for (int x = leftPosition; x <= rightPosition; x++)
{
if (bmp.GetPixel(x, y).ToArgb() != backColorA...
{
topPosition = y;
break;
}
}
if (0 <= topPosition)
{
break;
}
}
if (topPosition < 0)
{
return Rectangle.Empty;
}
//下の空白部分を計測する
int bottomPosition = -1;
for (int y = maxHeight - 1; topPosition < y; y--)
{
for (int x = leftPosition; x <= rightPosition; x++)
{
if (bmp.GetPixel(x, y).ToArgb() != backColorA...
{
bottomPosition = y;
break;
}
}
if (0 <= bottomPosition)
{
break;
}
}
if (bottomPosition < 0)
{
bottomPosition = topPosition;
}
//結果を返す
return new Rectangle(leftPosition, topPosition,
rightPosition - leftPosition, bottomPosition - to...
}
private static Rectangle MeasureForegroundArea(Bitmap bmp)
{
return MeasureForegroundArea(bmp, bmp.GetPixel(0, 0));
}
}}
**使用例 [#p1a49a00]
上記MeasureStringPreciselyメソッドを使用した例を示します...
なおこの例では、「PictureBox1」というPictureBoxコントロー...
#code(vbnet){{
'Imports System.Drawing
'表示する文字列
Dim s As String = "DOBON.NET"
'描画先とするImageオブジェクトを作成する
Dim canvas As New Bitmap(PictureBox1.Width, PictureBox1.H...
'ImageオブジェクトのGraphicsオブジェクトを作成する
Dim g As Graphics = Graphics.FromImage(canvas)
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode....
g.TextRenderingHint = System.Drawing.Text.TextRenderingHi...
'フォントオブジェクトの作成
Dim fnt As New Font("Arial", 25, FontStyle.Italic)
'StringFormatオブジェクトの作成
Dim sf As New StringFormat()
'幅の最大値が1000ピクセルとして、文字列を描画するときの大...
Dim stringSize As SizeF = g.MeasureString(s, fnt, 1000, sf)
'取得した文字列の大きさを使って四角を描画する
g.DrawRectangle(Pens.Red, 0, 0, stringSize.Width, stringS...
'より正確に大きさを計測する
Dim rect As Rectangle = MeasureStringPrecisely(g, s, fnt,...
g.DrawRectangle(Pens.Blue, rect.Left, rect.Top, rect.Widt...
'文字列を描画する
g.DrawString(s, fnt, Brushes.Black, 0, 0, sf)
'リソースを解放する
fnt.Dispose()
sf.Dispose()
g.Dispose()
'PictureBox1に表示する
PictureBox1.Image = canvas
}}
#code(csharp){{
//using System.Drawing;
//表示する文字列
string s = "DOBON.NET";
//描画先とするImageオブジェクトを作成する
Bitmap canvas = new Bitmap(PictureBox1.Width, PictureBox1...
//ImageオブジェクトのGraphicsオブジェクトを作成する
Graphics g = Graphics.FromImage(canvas);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode....
g.TextRenderingHint = System.Drawing.Text.TextRenderingHi...
//フォントオブジェクトの作成
Font fnt = new Font("Arial", 25, FontStyle.Italic);
//StringFormatオブジェクトの作成
StringFormat sf = new StringFormat();
//幅の最大値が1000ピクセルとして、文字列を描画するときの...
SizeF stringSize = g.MeasureString(s, fnt, 1000, sf);
//取得した文字列の大きさを使って四角を描画する
g.DrawRectangle(Pens.Red, 0, 0, stringSize.Width, stringS...
//より正確に大きさを計測する
Rectangle rect = MeasureStringPrecisely(g, s, fnt, canvas...
g.DrawRectangle(Pens.Blue, rect.Left, rect.Top, rect.Widt...
//文字列を描画する
g.DrawString(s, fnt, Brushes.Black, 0, 0, sf);
//リソースを解放する
fnt.Dispose();
sf.Dispose();
g.Dispose();
//PictureBox1に表示する
PictureBox1.Image = canvas;
}}
上記コードを実行した結果は、例えば以下の画像のようになり...
&ref(measurestring4.png);
**コードの説明 [#l3ebed98]
MeasureStringPreciselyメソッドで何をしているのか、簡単に...
まず十分な大きさのBitmapオブジェクトを作成します。ここに...
Bitmapオブジェクトを作成する時に、文字列の描画先であるGra...
次に、BitmapのGraphicsを作成しています。そして、Bitmapに...
準備が整ったところで、Bitmapに文字列を実際に描画します。...
ここでいよいよ計測ですが、これは別のメソッド(MeasureFore...
ちなみに色を比較する時は、Color.ToArgbメソッドでARGB値に...
**MeasureStringPreciselyメソッドのproposedSizeパラメータ...
MeasureStringPreciselyメソッドのproposedSizeパラメータに...
それではどの位の大きさが適当なのかについては、Graphics.Me...
万全を期すならば、さらにMeasureStringPreciselyメソッドが...
**最後に [#se73f406]
上記のメソッドはまだテストが十分とは言えませんので、不完...
**参考 [#b8b2305c]
-[[文字列を描画したときの大きさを計測する>https://dobon.n...
**コメント [#baef593e]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2014-0...
終了行:
#title(文字列を描画したときの大きさを正確に計測する)
#navi(.NETプログラミング研究)
#contents
*文字列を描画したときの大きさを正確に計測する [#x9b591fe]
**.NET Frameworkで用意されている3つの方法 [#f8c110bc]
Graphics.DrawStringメソッドやTextRenderer.DrawTextメソッ...
Graphics.MeasureStringメソッドを呼び出す時にStringFormat....
**実際に描画することで、正確に計測する [#fc0ee2f2]
実際に描画される大きさを正確に計測するには、やはり実際に...
これを参考にさせていただいて、私も以下のようなコードを書...
**コード [#f43ce5e5]
#code(vbnet){{
'Imports System.Drawing
''' <summary>
''' Graphics.DrawStringで文字列を描画した時の大きさと位置...
''' </summary>
''' <param name="g">文字列を描画するGraphics</param>
''' <param name="text">描画する文字列</param>
''' <param name="font">描画に使用するフォント</param>
''' <param name="proposedSize">これ以上大きいことはないと...
''' できるだけ小さくすること。</param>
''' <param name="stringFormat">描画に使用するStringFormat...
''' <returns>文字列が描画される範囲。
''' 見つからなかった時は、Rectangle.Empty。</returns>
Public Shared Function MeasureStringPrecisely(g As Graphi...
text As String, font As Font, proposedSize As Siz...
stringFormat As StringFormat) As Rectangle
'解像度を引き継いで、Bitmapを作成する
Dim bmp As New Bitmap(proposedSize.Width, proposedSiz...
'BitmapのGraphicsを作成する
Dim bmpGraphics As Graphics = Graphics.FromImage(bmp)
'Graphicsのプロパティを引き継ぐ
bmpGraphics.TextRenderingHint = g.TextRenderingHint
bmpGraphics.TextContrast = g.TextContrast
bmpGraphics.PixelOffsetMode = g.PixelOffsetMode
'文字列の描かれていない部分の色を取得する
Dim backColor As Color = bmp.GetPixel(0, 0)
'実際にBitmapに文字列を描画する
bmpGraphics.DrawString(text, font, Brushes.Black, _
New RectangleF(0.0F, 0.0F, proposedSize.Width, pr...
stringFormat)
bmpGraphics.Dispose()
'文字列が描画されている範囲を計測する
Dim resultRect As Rectangle = MeasureForegroundArea(b...
bmp.Dispose()
Return resultRect
End Function
''' <summary>
''' 指定されたBitmapで、backColor以外の色が使われている範...
''' </summary>
Private Shared Function MeasureForegroundArea(bmp As Bitm...
backColor As Color) As Rectangle
Dim backColorArgb As Integer = backColor.ToArgb()
Dim maxWidth As Integer = bmp.Width
Dim maxHeight As Integer = bmp.Height
Dim x As Integer
Dim y As Integer
'左側の空白部分を計測する
Dim leftPosition As Integer = -1
For x = 0 To maxWidth - 1
For y = 0 To maxHeight - 1
'違う色を見つけたときは、位置を決定する
If bmp.GetPixel(x, y).ToArgb() <> backColorAr...
leftPosition = x
Exit For
End If
Next
If 0 <= leftPosition Then
Exit For
End If
Next
'違う色が見つからなかった時
If leftPosition < 0 Then
Return Rectangle.Empty
End If
'右側の空白部分を計測する
Dim rightPosition As Integer = -1
For x = maxWidth - 1 To leftPosition + 1 Step -1
For y = 0 To maxHeight - 1
If bmp.GetPixel(x, y).ToArgb() <> backColorAr...
rightPosition = x
Exit For
End If
Next
If 0 <= rightPosition Then
Exit For
End If
Next
If rightPosition < 0 Then
rightPosition = leftPosition
End If
'上の空白部分を計測する
Dim topPosition As Integer = -1
For y = 0 To maxHeight - 1
For x = leftPosition To rightPosition
If bmp.GetPixel(x, y).ToArgb() <> backColorAr...
topPosition = y
Exit For
End If
Next
If 0 <= topPosition Then
Exit For
End If
Next
If topPosition < 0 Then
Return Rectangle.Empty
End If
'下の空白部分を計測する
Dim bottomPosition As Integer = -1
For y = maxHeight - 1 To topPosition + 1 Step -1
For x = leftPosition To rightPosition
If bmp.GetPixel(x, y).ToArgb() <> backColorAr...
bottomPosition = y
Exit For
End If
Next
If 0 <= bottomPosition Then
Exit For
End If
y -= 1
Next
If bottomPosition < 0 Then
bottomPosition = topPosition
End If
'結果を返す
Return New Rectangle(leftPosition, topPosition, _
rightPosition - leftPosition, bottomPosition - to...
End Function
Private Shared Function MeasureForegroundArea(bmp As Bitm...
Return MeasureForegroundArea(bmp, bmp.GetPixel(0, 0))
End Function
}}
#code(csharp){{
//using System.Drawing;
/// <summary>
/// Graphics.DrawStringで文字列を描画した時の大きさと位置...
/// </summary>
/// <param name="g">文字列を描画するGraphics</param>
/// <param name="text">描画する文字列</param>
/// <param name="font">描画に使用するフォント</param>
/// <param name="proposedSize">これ以上大きいことはないと...
/// できるだけ小さくすること。</param>
/// <param name="stringFormat">描画に使用するStringFormat...
/// <returns>文字列が描画される範囲。
/// 見つからなかった時は、Rectangle.Empty。</returns>
public static Rectangle MeasureStringPrecisely(Graphics g,
string text, Font font, Size proposedSize, StringForm...
{
//解像度を引き継いで、Bitmapを作成する
Bitmap bmp = new Bitmap(proposedSize.Width, proposedS...
//BitmapのGraphicsを作成する
Graphics bmpGraphics = Graphics.FromImage(bmp);
//Graphicsのプロパティを引き継ぐ
bmpGraphics.TextRenderingHint = g.TextRenderingHint;
bmpGraphics.TextContrast = g.TextContrast;
bmpGraphics.PixelOffsetMode = g.PixelOffsetMode;
//文字列の描かれていない部分の色を取得する
Color backColor = bmp.GetPixel(0, 0);
//実際にBitmapに文字列を描画する
bmpGraphics.DrawString(text, font, Brushes.Black,
new RectangleF(0f, 0f, proposedSize.Width, propos...
stringFormat);
bmpGraphics.Dispose();
//文字列が描画されている範囲を計測する
Rectangle resultRect = MeasureForegroundArea(bmp, bac...
bmp.Dispose();
return resultRect;
}
/// <summary>
/// 指定されたBitmapで、backColor以外の色が使われている範...
/// </summary>
private static Rectangle MeasureForegroundArea(Bitmap bmp...
{
int backColorArgb = backColor.ToArgb();
int maxWidth = bmp.Width;
int maxHeight = bmp.Height;
//左側の空白部分を計測する
int leftPosition = -1;
for (int x = 0; x < maxWidth; x++)
{
for (int y = 0; y < maxHeight; y++)
{
//違う色を見つけたときは、位置を決定する
if (bmp.GetPixel(x, y).ToArgb() != backColorA...
{
leftPosition = x;
break;
}
}
if (0 <= leftPosition)
{
break;
}
}
//違う色が見つからなかった時
if (leftPosition < 0)
{
return Rectangle.Empty;
}
//右側の空白部分を計測する
int rightPosition = -1;
for (int x = maxWidth - 1; leftPosition < x; x--)
{
for (int y = 0; y < maxHeight; y++)
{
if (bmp.GetPixel(x, y).ToArgb() != backColorA...
{
rightPosition = x;
break;
}
}
if (0 <= rightPosition)
{
break;
}
}
if (rightPosition < 0)
{
rightPosition = leftPosition;
}
//上の空白部分を計測する
int topPosition = -1;
for (int y = 0; y < maxHeight; y++)
{
for (int x = leftPosition; x <= rightPosition; x++)
{
if (bmp.GetPixel(x, y).ToArgb() != backColorA...
{
topPosition = y;
break;
}
}
if (0 <= topPosition)
{
break;
}
}
if (topPosition < 0)
{
return Rectangle.Empty;
}
//下の空白部分を計測する
int bottomPosition = -1;
for (int y = maxHeight - 1; topPosition < y; y--)
{
for (int x = leftPosition; x <= rightPosition; x++)
{
if (bmp.GetPixel(x, y).ToArgb() != backColorA...
{
bottomPosition = y;
break;
}
}
if (0 <= bottomPosition)
{
break;
}
}
if (bottomPosition < 0)
{
bottomPosition = topPosition;
}
//結果を返す
return new Rectangle(leftPosition, topPosition,
rightPosition - leftPosition, bottomPosition - to...
}
private static Rectangle MeasureForegroundArea(Bitmap bmp)
{
return MeasureForegroundArea(bmp, bmp.GetPixel(0, 0));
}
}}
**使用例 [#p1a49a00]
上記MeasureStringPreciselyメソッドを使用した例を示します...
なおこの例では、「PictureBox1」というPictureBoxコントロー...
#code(vbnet){{
'Imports System.Drawing
'表示する文字列
Dim s As String = "DOBON.NET"
'描画先とするImageオブジェクトを作成する
Dim canvas As New Bitmap(PictureBox1.Width, PictureBox1.H...
'ImageオブジェクトのGraphicsオブジェクトを作成する
Dim g As Graphics = Graphics.FromImage(canvas)
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode....
g.TextRenderingHint = System.Drawing.Text.TextRenderingHi...
'フォントオブジェクトの作成
Dim fnt As New Font("Arial", 25, FontStyle.Italic)
'StringFormatオブジェクトの作成
Dim sf As New StringFormat()
'幅の最大値が1000ピクセルとして、文字列を描画するときの大...
Dim stringSize As SizeF = g.MeasureString(s, fnt, 1000, sf)
'取得した文字列の大きさを使って四角を描画する
g.DrawRectangle(Pens.Red, 0, 0, stringSize.Width, stringS...
'より正確に大きさを計測する
Dim rect As Rectangle = MeasureStringPrecisely(g, s, fnt,...
g.DrawRectangle(Pens.Blue, rect.Left, rect.Top, rect.Widt...
'文字列を描画する
g.DrawString(s, fnt, Brushes.Black, 0, 0, sf)
'リソースを解放する
fnt.Dispose()
sf.Dispose()
g.Dispose()
'PictureBox1に表示する
PictureBox1.Image = canvas
}}
#code(csharp){{
//using System.Drawing;
//表示する文字列
string s = "DOBON.NET";
//描画先とするImageオブジェクトを作成する
Bitmap canvas = new Bitmap(PictureBox1.Width, PictureBox1...
//ImageオブジェクトのGraphicsオブジェクトを作成する
Graphics g = Graphics.FromImage(canvas);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode....
g.TextRenderingHint = System.Drawing.Text.TextRenderingHi...
//フォントオブジェクトの作成
Font fnt = new Font("Arial", 25, FontStyle.Italic);
//StringFormatオブジェクトの作成
StringFormat sf = new StringFormat();
//幅の最大値が1000ピクセルとして、文字列を描画するときの...
SizeF stringSize = g.MeasureString(s, fnt, 1000, sf);
//取得した文字列の大きさを使って四角を描画する
g.DrawRectangle(Pens.Red, 0, 0, stringSize.Width, stringS...
//より正確に大きさを計測する
Rectangle rect = MeasureStringPrecisely(g, s, fnt, canvas...
g.DrawRectangle(Pens.Blue, rect.Left, rect.Top, rect.Widt...
//文字列を描画する
g.DrawString(s, fnt, Brushes.Black, 0, 0, sf);
//リソースを解放する
fnt.Dispose();
sf.Dispose();
g.Dispose();
//PictureBox1に表示する
PictureBox1.Image = canvas;
}}
上記コードを実行した結果は、例えば以下の画像のようになり...
&ref(measurestring4.png);
**コードの説明 [#l3ebed98]
MeasureStringPreciselyメソッドで何をしているのか、簡単に...
まず十分な大きさのBitmapオブジェクトを作成します。ここに...
Bitmapオブジェクトを作成する時に、文字列の描画先であるGra...
次に、BitmapのGraphicsを作成しています。そして、Bitmapに...
準備が整ったところで、Bitmapに文字列を実際に描画します。...
ここでいよいよ計測ですが、これは別のメソッド(MeasureFore...
ちなみに色を比較する時は、Color.ToArgbメソッドでARGB値に...
**MeasureStringPreciselyメソッドのproposedSizeパラメータ...
MeasureStringPreciselyメソッドのproposedSizeパラメータに...
それではどの位の大きさが適当なのかについては、Graphics.Me...
万全を期すならば、さらにMeasureStringPreciselyメソッドが...
**最後に [#se73f406]
上記のメソッドはまだテストが十分とは言えませんので、不完...
**参考 [#b8b2305c]
-[[文字列を描画したときの大きさを計測する>https://dobon.n...
**コメント [#baef593e]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2014-0...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]