1. 程式人生 > >[譯]如何在.NET Core中使用System.Drawing?

[譯]如何在.NET Core中使用System.Drawing?

web led TE 執行 strip sky sha otl nes

你大概知道System.Drawing,它是一個執行圖形相關任務的流行的API,同時它也不屬於.NET Core的一部分。最初是把.NET Core作為雲端框架設計的,它不包含非雲端相關API。另一方面,.NET Core是跨平臺框架,它不包含任何操作系統特定的API,例如Windows上的Active Directory活動目錄 。再者,.NET Core也不包括類似,作為.NET framework一部分的,ConfigurationManager這樣的諸多API。

在我之前的文章中,我已經描述了如何將這些API用於.NET Core和.NET標準應用程序。

為了實現這一點,有一個稱為Windows Compatibility Pack

的元包。但是,這個元包包含許多與Windows相關的API(作為包分發)。在這個元包裏面有一個System.Drawing.Common包。

為了在.NET Core中使用繪圖功能,請在Nuget包添加引用:

<PackageReference Include="System.Drawing.Common" Version="4.5.0-preview1-**" >

在這之後,你將可以運行例如以下的代碼。

int x = 100;
Image image = new Bitmap(2000, 1024);

Graphics graph = Graphics.FromImage(image);

graph.Clear(Color.Azure);

Pen pen = new Pen(Brushes.Black);

graph.DrawLines(pen, new Point[] { new Point(10,10), new Point(800, 900) });

graph.DrawString("Hello drawing from .NET Core :)", 
new Font(new FontFamily("DecoType Thuluth"), 20,  FontStyle.Bold), 
Brushes.Blue, new PointF(150, 90));

image.Save("graph.jpeg", System.Drawing.Imaging.ImageFormat.Png);

在你運行上述代碼之後,你將看到如下圖片:
技術分享圖片

下面是當前已經支持的字體列表:

AGA Arabesque
AGA Arabesque Desktop
Agency FB
Akhbar MT
Algerian
Arial
Arial Black
Arial Narrow
Arial Rounded MT Bold
Bahnschrift
Bahnschrift Light
Bahnschrift SemiBold
Bahnschrift SemiLight
Baskerville Old Face
Bauhaus 93
Bell MT
Berlin Sans FB
Berlin Sans FB Demi
Bernard MT Condensed
Blackadder ITC
Bodoni MT
Bodoni MT Black
Bodoni MT Condensed
Bodoni MT Poster Compressed
Bold Italic Art
Book Antiqua
Bookman Old Style
Bookshelf Symbol 7
Bradley Hand ITC
Britannic Bold
Broadway
Brush Script MT
Calibri
Calibri Light
Californian FB
Calisto MT
Cambria
Cambria Math
Candara
Castellar
Centaur
Century
Century Gothic
Century Schoolbook
Chiller
Colonna MT
Comic Sans MS
Consolas
Constantia
Cooper Black
Copperplate Gothic Bold
Copperplate Gothic Light
Corbel
Courier New
Curlz MT
DecoType Naskh
DecoType Naskh Extensions
DecoType Naskh Special
DecoType Naskh Swashes
DecoType Naskh Variants
DecoType Thuluth
Diwani Bent
Diwani Letter
Diwani Outline Shaded
Diwani Simple Outline
Diwani Simple Outline 2
Diwani Simple Striped
Dosis
Dubai
Dubai Light
Dubai Medium
Ebrima
Edwardian Script ITC
Elephant
Engravers MT
Eras Bold ITC
Eras Demi ITC
Eras Light ITC
Eras Medium ITC
Farsi Simple Bold
Farsi Simple Outline
Felix Titling
Footlight MT Light
Forte
Franklin Gothic Book
Franklin Gothic Demi
Franklin Gothic Demi Cond
Franklin Gothic Heavy
Franklin Gothic Medium
Franklin Gothic Medium Cond
Freestyle Script
French Script MT
FZShuTi
FZYaoTi
Gabriola
Gadugi
Garamond
Georgia
Gigi
Gill Sans MT
Gill Sans MT Condensed
Gill Sans MT Ext Condensed Bold
Gill Sans Ultra Bold
Gill Sans Ultra Bold Condensed
Gloucester MT Extra Condensed
Goudy Old Style
Goudy Stout
Haettenschweiler
Harlow Solid Italic
Harrington
High Tower Text
HoloLens MDL2 Assets
Impact
Imprint MT Shadow
Informal Roman
Italic Outline Art
Javanese Text
Jokerman
Juice ITC
Kristen ITC
Kufi Extended Outline
Kufi Outline Shaded
Kunstler Script
Lato
Lato Black
Led Italic Font
Leelawadee UI
Leelawadee UI Semilight
LiSu
Lucida Bright
Lucida Calligraphy
Lucida Console
Lucida Fax
Lucida Handwriting
Lucida Sans
Lucida Sans Typewriter
Lucida Sans Unicode
Magneto
Maiandra GD
Malgun Gothic
Malgun Gothic Semilight
Marlett
Matura MT Script Capitals
Microsoft Himalaya
Microsoft JhengHei
Microsoft JhengHei Light
Microsoft JhengHei UI
Microsoft JhengHei UI Light
Microsoft New Tai Lue
Microsoft Office Preview Font
Microsoft PhagsPa
Microsoft Sans Serif
Microsoft Tai Le
Microsoft YaHei
Microsoft YaHei Light
Microsoft YaHei UI
Microsoft YaHei UI Light
Microsoft Yi Baiti
MingLiU-ExtB
MingLiU_HKSCS-ExtB
Mistral
Modern No. 20
Mongolian Baiti
Monotype Corsiva
Monotype Koufi
Montserrat
MS Gothic
MS Office Symbol (de-de) Bold
MS Office Symbol (de-de) Light
MS Office Symbol Extralight
MS Office Symbol Regular
MS Office Symbol Semibold
MS Office Symbol Semilight
MS Outlook
MS PGothic
MS Reference Sans Serif
MS Reference Specialty
MS UI Gothic
MT Extra
Mudir MT
MV Boli
Myanmar Text
Niagara Engraved
Niagara Solid
Nirmala UI
Nirmala UI Semilight
NSimSun
OCR A Extended
Old Antic Bold
Old Antic Decorative
Old Antic Outline
Old Antic Outline Shaded
Old English Text MT
Onyx
Oswald
Palace Script MT
Palatino Linotype
Papyrus
Parchment
Perpetua
Perpetua Titling MT
Playbill
PMingLiU-ExtB
Poor Richard
Pristina
PT Bold Arch
PT Bold Broken
PT Bold Dusky
PT Bold Heading
PT Bold Mirror
PT Bold Stars
PT Separated Baloon
PT Simple Bold Ruled
Rage Italic
Ravie
Rockwell
Rockwell Condensed
Rockwell Extra Bold
Script MT Bold
Segoe MDL2 Assets
Segoe Print
Segoe Script
Segoe UI
Segoe UI Black
Segoe UI Emoji
Segoe UI Historic
Segoe UI Light
Segoe UI Semibold
Segoe UI Semilight
Segoe UI Symbol
Showcard Gothic
Simple Bold Jut Out
Simple Indust Outline
Simple Indust Shaded
Simple Outline Pat
SimSun
SimSun-ExtB
Sitka Banner
Sitka Display
Sitka Heading
Sitka Small
Sitka Subheading
Sitka Text
Snap ITC
Source Sans Pro
Source Sans Pro Black
Source Sans Pro Semibold
STCaiyun
Stencil
STFangsong
STHupo
STKaiti
STLiti
STSong
STXihei
STXingkai
STXinwei
STZhongsong
Sylfaen
Symbol
Tahoma
Tempus Sans ITC
Times New Roman
Trebuchet MS
Tw Cen MT
Tw Cen MT Condensed
Tw Cen MT Condensed Extra Bold
Verdana
Viner Hand ITC
Vivaldi
Vladimir Script
Webdings
Wide Latin
Wingdings
Wingdings 2
Wingdings 3
YouYuan
Yu Gothic
Yu Gothic Light
Yu Gothic Medium
Yu Gothic UI
Yu Gothic UI Light
Yu Gothic UI Semibold
Yu Gothic UI Semilight

翻譯自:How to use System.Drawing in .NET Core?

[譯]如何在.NET Core中使用System.Drawing?