久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在精確的像素位置繪制字符串

How to draw a string at an exact pixel position(如何在精確的像素位置繪制字符串)
本文介紹了如何在精確的像素位置繪制字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我嘗試將 C# 中的字符串(單個字符)繪制到位圖的精確位置:

I try to draw a string (single character) in C# into a Bitmap at an exact position with:

Bitmap bmp = new Bitmap(64, 64);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("W", font1, new SolidBrush(myColor), new Point(32,32);

在單個字母周圍渲染了太多空白空間,我無法猜測繪制角色的所需"位置以使其位于末尾的正確位置.

There is so much empty space rendered around a single letter, that I can not guess the "needed" position to draw the character to have it at the correct position at the end.

現在我有了字符的像素精確尺寸(查看單獨呈現的位圖中的位).但是如果我不能在一個確切的位置(例如中心或右上角或....)繪制字符,這些信息是無用的.

By now I have the pixel exact dimension of the character (looking at bits in a separately rendered bitmap). But this information is useless, if I cannot draw the character at an exact position (e.g. center or top right corner or ....).

是否有其他方法可以在 C# 中在位圖上繪制文本?或者是否有任何轉換方法可以轉換 DrawString 需要的實際像素位置?

Are there other methods to draw text in C# on a bitmap? Or are there any converting methods to convert the real pixel position in something DrawString needs?

推薦答案

無需查看像素或開始使用您自己的字體..

No need to look at the pixels or start working with your own font..

您可以使用 GraphicsPath 而不是 DrawStringTextRenderer,因為它會讓您知道它的網絡邊界矩形GraphicsPath.GetBounds() .

You can use a GraphicsPath instead of DrawString or TextRenderer, as it will let you know its net bounds rectangle with GraphicsPath.GetBounds() .

知道了之后,就可以使用 TranslateTransform 計算如何移動 Graphics 對象:

When you know it, you can calculate how to move the Graphics object using TranslateTransform:

private void button1_Click(object sender, EventArgs e)
{
    string text = "Y";                  // whatever
    Bitmap bmp = new Bitmap(64, 64);    // whatever
    bmp.SetResolution(96, 96);          // whatever
    float fontSize = 32f;               // whatever

    using ( Graphics g = Graphics.FromImage(bmp))
    using ( GraphicsPath GP = new GraphicsPath())
    using ( FontFamily fontF = new FontFamily("Arial"))
    {
        testPattern(g, bmp.Size);      // optional

        GP.AddString(text, fontF, 0, fontSize, Point.Empty,
                     StringFormat.GenericTypographic);
        // this is the net bounds without any whitespace:
        Rectangle br = Rectangle.Round(GP.GetBounds());

        g.DrawRectangle(Pens.Red,br); // just for testing

        // now we center:
        g.TranslateTransform( (bmp.Width - br.Width )  / 2 - br.X,
                              (bmp.Height - br.Height )/ 2 - br.Y);
        // and fill
        g.FillPath(Brushes.Black, GP);
        g.ResetTransform();
    }

    // whatever you want to do..
    pictureBox1.Image = bmp;
    bmp.Save("D:\__test.png", ImageFormat.Png);

}

一個小測試例程,讓我們更好地看到居中:

A small test routine to let us see the centering better:

void testPattern(Graphics g, Size sz)
{
    List<Brush> brushes = new List<Brush>() 
    {   Brushes.SlateBlue, Brushes.Yellow, 
        Brushes.DarkGoldenrod, Brushes.Lavender };
    int bw2 = sz.Width / 2;
    int bh2 = sz.Height / 2;
    for (int i = bw2; i > 0; i--)
        g.FillRectangle(brushes[i%4],bw2 - i, bh2 - i, i + i, i + i );

}

GetBounds 方法返回一個 RectangleF;在我的示例中,它是 {X=0.09375, Y=6.0625, Width=21, Height=22.90625}.請注意,由于四舍五入,事物總是可以減去一.

The GetBounds method returns a RectangleF; in my example it is {X=0.09375, Y=6.0625, Width=21, Height=22.90625}. Do note that due to rounding things can always be off by one..

您可能想也可能不想將 Graphics 設置更改為特殊的 Smoothingmodes 等.

You may or may not want to change the Graphics setting to special Smoothingmodes etc..

還應注意,這將自動進行,即通過邊界矩形進行機械居中.這可能與 'optical or視覺居中',這很難編碼,并且在某種程度上是個人品味的問題.但排版既是一種職業,也是一種藝術..

Also it should be noted that this will do automatic ie mechanical centering by the bounds rectangle. This may be quite different from 'optical or visual centering', which is rather hard to code and to some extent a matter of personal taste. But typography is as much an art as a profession..

這篇關于如何在精確的像素位置繪制字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to add an icon to System.Windows.Forms.MenuItem?(如何向 System.Windows.Forms.MenuItem 添加圖標?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
Building a database driven menu with ASP.NET, JQuery and Suckerfish(使用 ASP.NET、JQuery 和 Suckerfish 構建數據庫驅動的菜單)
主站蜘蛛池模板: 欧美日韩在线一区二区 | 日韩欧美在线视频观看 | 亚洲狠狠干 | 欧美伊人网 | 黄色福利视频 | 欧美视频在线观看一区 | 国产精品久久久久久久久 | 国产伦精品一区二区三区88av | 日韩av在线免费播放 | 黄色成人小视频 | 小日子的在线观看免费第8集 | 黄色录像一级片 | aaa国产精品 | 免费观看a级片 | 天天色影院 | 手机av免费| 欧美在线 | 日韩黄色在线视频 | 中文字幕一二三四区 | av福利在线| 一级片黄色 | 精品乱子伦一区二区三区 | 日韩一级片视频 | 亚洲久久在线 | 九九热在线精品 | 国产高清在线观看 | 久久久久久艹 | 国产一区二区视频在线 | 国产一区不卡 | 黄色录像一级片 | 欧美激情一区二区 | 亚洲网在线| 深夜久久 | 毛片在线观看视频 | 国产一区久久 | 黄色大片在线免费观看 | 亚洲成人福利 | 亚洲福利一区 | 精品国产乱码久久久久久蜜柚 | 91中文| 少妇综合 |