小さなアプリケーションを書いて、指定したテキストを画像の最後に追加しようとしています。このテキストは元のイメージの上に追加することはできず、元のイメージのフレームサイズに加えて配置する必要があります。vb.netを使用してイメージにテキストを追加します。フォントサイズ計算のエラー
これはほとんどの場合うまく動作しますが、別のカメラを使用してテストした場合、同じ結果がスクリプトで生成されましたが、画像の解像度がはるかに優れていたためです。フォントはあまりにも小さくて信憑性がありませんでした。
必要なフォントサイズを計算するためのコーディングを追加しましたが、これは小さい解像度の画像ではうまくいくように見えますが、高品質の画像には大きすぎるフォントが生成されます。
これは、フォントサイズを計算するための私のコード、saveコマンドがオリジナルの圧縮ステータス、または私が考慮していない他のものの圧縮ステータスを変更したことが原因である可能性があります。
いくつかのガイダンスがずっとappreciteだろう...
Dim objImage As Bitmap
Dim objNewImage As Bitmap
Dim objGraphics As Graphics
Dim objFont As Font
Dim szTextSize As Size
Dim intPictureHeightToAdd As Integer
Dim emFontSize As Single
' load image passed as full path string, return 1 if failure
objImage = Bitmap.FromFile(v_strFullFileName, True)
' height to add will be 5%
intPictureHeightToAdd = (objImage.Height * 5)/100
' create a new image with the same dimensions plus space for text and same pixel format
objNewImage = New Bitmap(objImage.Width, objImage.Height + intPictureHeightToAdd, objImage.PixelFormat)
' set the resolution to the same as existing
objNewImage.SetResolution(objImage.HorizontalResolution, objImage.VerticalResolution)
' create a graphic object for image manipultion
objGraphics = objGraphics.FromImage(objNewImage)
' place existing image into graphic top left. This will leave space at bottom for text
objGraphics.DrawImage(objImage, 0, 0)
' add meta data to intended new image from existing
For Each propItem In objImage.PropertyItems
objNewImage.SetPropertyItem(propItem)
Next propItem
' create the font and measure this, comparing to height of image. (if image is to large, and font too small; it will be unredable)
emFontSize = 10.0F
Do
emFontSize += 1
objFont = New Font("Courier", emFontSize)
szTextSize = TextRenderer.MeasureText(m_strTextToAppend, objFont)
Loop Until szTextSize.Height >= intPictureHeightToAdd
emFontSize -= 10
objFont = New Font("Courier", emFontSize)
' draw a rectangle with text in space remaining (set text to red as most likely to stand out in background
objGraphics.DrawString(m_strTextToAppend, objFont, Brushes.Red, New RectangleF(0, objImage.Height, objImage.Width, intPictureHeightToAdd))
objImage.Dispose()
' save font added
objNewImage.Save(v_strFullFileName, Imaging.ImageFormat.Jpeg)
'dispose of new image as now saved and graphics object
objNewImage.Dispose()
objGraphics.Dispose()
困ったときは、フォントが画像の高さの5%を超えていますか?どれくらい大きい? –
それははるかに大きいです。グラフィックの矩形オブジェクトよりも大きく、フォント全体が見えないのは画像に部分的にしか描画されていないからです... –
初期化時にフォントのグラフィックユニットをピクセルに設定することがあります。 MeasureTextはイメージのdpiを認識せず、異なるdpiでemを使用すると問題が発生する可能性があります。 –