2012-01-31 25 views
1

イメージにg.DrawStringという文字列を描画しています。私がこれをやる前に、私はソースイメージ上にイメージを描画しています。奇妙なDrawStringの動作

これは私のコードは次のようになります。

Bitmap sourceBitmap = Image.FromFile(myBitmap) as Bitmap; 
Graphics g = Graphics.FromImage(sourceBitmap); 

// drawing some other images on sourceBitmap with g.DrawImage 

int positionx = 100; 
Font ftemp = cvt.ConvertFromString(myfont) as Font; // predefined font 
SizeF mysize = TextRenderer.MeasureText(mytext, ftemp); 
PointF myposition = new PointF(positionx - mysize.Width, positiony) // align right 
g.DrawString(mytext, ftemp, new SolidBrush(mycolor), myposition); 

私が得る何が:enter image description here

私は今、このようにすでに述べたように、コードの前に次の2行を追加する場合:

Bitmap sourceBitmap = Image.FromFile(myBitmap) as Bitmap; 
Graphics g = Graphics.FromImage(sourceBitmap); 

// drawing some other images on sourceBitmap with g.DrawImage 

sourceBitmap = new Bitmap(sourceBitmap); // sourceBitmap is the bitmap (png) I'm drawing on from the very beginning. 
g = Graphics.FromImage((Image)sourceBitmap); 

int positionx = 100; 
Font ftemp = cvt.ConvertFromString(myfont) as Font; // predefined font 
SizeF mysize = TextRenderer.MeasureText(mytext, ftemp); 
PointF myposition = new PointF(positionx - mysize.Width, positiony) // align right 
g.DrawString(mytext, ftemp, new SolidBrush(mycolor), myposition); 

私はこれを得る:enter image description here

私はこれらの2つの線以外に何も変えなかった。だから私の質問は、これがどのように起こっているかということです。

+1

よくwherreは最初のバージョンからの 'g'ですか? –

+0

sourceBitmapからも。 –

答えて

1

ソースPNGのDPIを確認してください。ファイルからビットマップをロードすると、ファイルに記録されたDPIが読み込まれます。すでにメモリ上にある別のビットマップに基づいて新しいビットマップを作成すると、そのビットマップは画面DPIにリセットされます。元のDPI設定を継承しません。

あなたのフォントは、次のようにGraphicsUnit.Pixelで表現するように指定することができます。

Font f = new Font("Arial", 10.0f, GraphicsUnit.Pixel); 

そうでなければ、あなたのフォントは、画像のDPIに合わせて拡張します。イメージDPIは、実際にイメージが想定される実際の単位(インチ)の大きさを定義するために使用されます。印刷のために拡大縮小するかどうかに応じて、テキストを拡大/縮小するときにその情報を使用する場合と使用しない場合があります。