c 獲得文字的畫素寬度

2022-03-12 11:17:52 字數 846 閱讀 7721

graphics graphics = creategraphics();  

sizef sizef = graphics.measurestring(textbox1.text, new font("宋體", 9));

messagebox.show(string.format("字型寬度:,高度:", sizef.width, sizef.height));

graphics.dispose();

使用g.measurestring()獲得

使用measurestring測量出來的字元寬度,總是比實際寬度大一些,而且隨著字元的長度增大,貌似實際寬度和測量寬度的差距也越來越大了。查了一下msdn,找到了下面這個理由:

measurestring

方法旨在與個別字串一起使用,它在字串前後包括少量額外的空格供突出的標誌符號使用。

string str;

str = "大";

font f = new font("simsun", 7f, system.drawing.fontstyle.regular);

graphics g = this.creategraphics();

//單位為mm

g.pageunit = graphicsunit.millimeter;

sizef sim = g.measurestring(str, f);

2、使用textrenderer.measuretext獲得,提供使用指定尺寸建立文字初始邊框時,使用指定的裝置上下文、字型和格式說明所繪製的指定文字的尺寸(以畫素為單位)。

private void print(object sender, painteventargs e)

c 計算空格寬度畫素 c 獲得文字的畫素寬度

graphics graphics creategraphics sizef sizef graphics.measurestring textbox1.text,new font 宋體 9 messagebox.show string.format 字型寬度 高度 sizef.width,size...

獲取img物件的高度寬度畫素

今天在重寫放大鏡的時候用到不同大小的,放大後的定位就出現問題了,思考後覺得應該利用原圖的寬高計算一下確定各種框的比例和引數等,而不是限制所使用的的尺寸,更改頁面的尺寸,於是又套了幾層函式,最終是解決了。js中對img物件的基本使用就是這些 var img new image img.src 路徑 然...

iOS swift 文字寬度的計算

如圖所示,需要sectionview的標題寬度可以動態變化 舉例說明 只需在tableview的 方法 func tableview tableview uitableview viewforheaderinsection section int uiview 然後在sectionview檔案裡 f...