翻譯|使用教程|編輯:周思宇|2023-04-17 11:00:51.107|閱讀 173 次
概述:在本文中,您將學習如何在 C# 中繪制形狀。我將介紹如何以編程方式在圖像上繪制直線、橢圓、弧和矩形。您可以輕松地將提供的代碼示例集成到您的 C# 應用程序中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Aspose.Imaging for .NET是一個可以讓開發人員可以創建、編輯、畫圖、轉換圖像的圖像處庫,提供了一些開發平臺原有功能基礎之上的一些新特性。它獨立于其他應用程序, Aspose.Imaging for .NET允許你在不安裝PhotoShop的情況下保存PSD格式。
Aspose.Imaging for .NET非常靈活、穩定和強大。它的很多特性和圖像處理例程能夠滿足絕大多數圖像制作的要求。像所有的Aspose組件一樣, Aspose.Imaging for .NET能很好地在Win Form and Web Form 下面運行,并且還支持Silverlight。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
經常需要通過繪制形狀來創建不同的圖形對象,包括圓形、直線、矩形等。這些形狀也可以用于圖像的注釋。在本文中,您將學習如何在 C# 中以編程方式繪制不同的形狀。我們將演示如何繪制直線、橢圓、圓弧和矩形并生成它們的圖像。
要繪制不同類型的形狀,我們將使用Aspose.Imaging for .NET。這是一個了不起的圖像編輯 API,提供了廣泛的功能來處理圖像和創建繪圖。您可以下載API 或從 NuGet 安裝它。
PM> Install-Package Aspose.Imaging
下面是在C#中畫線的步驟。
下面的代碼示例顯示了如何在 C# 中繪制一條線。
// Create BmpOptions BmpOptions bmpCreateOptions = new BmpOptions(); bmpCreateOptions.BitsPerPixel = 32; // Define the source property for the instance of BmpOptions bmpCreateOptions.Source = new StreamSource(); // Creates an instance of Image and call create method by passing the // bmpCreateOptions object Image image = Image.Create(bmpCreateOptions, 500, 500); // Create and initialize an instance of Graphics class Graphics graphic = new Graphics(image); // Clear the image surface with White color graphic.Clear(Color.White); // Draw a dotted line by specifying the Pen object having blue color and // co-ordinate Points graphic.DrawLine(new Pen(Color.Blue, 3), 18, 18, 200, 200); graphic.DrawLine(new Pen(Color.Blue, 3), 18, 200, 200, 18); // Draw a continuous line by specifying the Pen object having Solid // Brush with red color and two point structures graphic.DrawLine(new Pen(new SolidBrush(Color.Red), 3), new Point(18, 18), new Point(18, 200)); // Draw a continuous line by specifying the Pen object having Solid // Brush with white color and two point structures graphic.DrawLine(new Pen(new SolidBrush(Color.Orange), 3), new Point(200, 18), new Point(18, 18)); // Save all changes image.Save("draw_lines.bmp");
以下是上述代碼示例的輸出。
下面是在C#中繪制橢圓的步驟。
下面的代碼示例顯示了如何在 C# 中的圖像上繪制橢圓。
// Create BmpOptions BmpOptions bmpCreateOptions = new BmpOptions(); bmpCreateOptions.BitsPerPixel = 32; // Define the source property for the instance of BmpOptions bmpCreateOptions.Source = new StreamSource(); // Creates an instance of Image and call create method by passing the // bmpCreateOptions object Image image = Image.Create(bmpCreateOptions, 500, 500); // Create and initialize an instance of Graphics class Graphics graphic = new Graphics(image); // Clear the image surface with White color graphic.Clear(Color.White); // Draw a dotted ellipse shape by specifying the Pen object having red // color and a surrounding Rectangle graphic.DrawEllipse(new Pen(Color.Red, 3), new Rectangle(60, 40, 70, 120)); // Draw a continuous ellipse shape by specifying the Pen object having // solid brush with blue color and a surrounding Rectangle graphic.DrawEllipse(new Pen(new SolidBrush(Color.Blue), 3), new Rectangle(40, 60, 120, 70)); // Save all changes image.Save("draw_ellipse.bmp");
以下是上述代碼示例的輸出。
下面是在C#中繪制圓弧的步驟。
下面的代碼示例顯示了如何在 C# 中的圖像上繪制圓弧。
// Create BmpOptions BmpOptions bmpCreateOptions = new BmpOptions(); bmpCreateOptions.BitsPerPixel = 32; // Define the source property for the instance of BmpOptions bmpCreateOptions.Source = new StreamSource(); // Creates an instance of Image and call create method by passing the // bmpCreateOptions object Image image = Image.Create(bmpCreateOptions, 500, 500); // Create and initialize an instance of Graphics class Graphics graphic = new Graphics(image); // Clear the image surface with White color graphic.Clear(Color.White); // Draw a dotted arc shape by specifying the Pen object having red black // color and coordinates, height, width, start & end angles int width = 200; int height = 300; int startAngle = 45; int sweepAngle = 270; // Draw arc to screen graphic.DrawArc(new Pen(Color.Black, 3), 0, 0, width, height, startAngle, sweepAngle); // Save all changes image.Save("draw_arc.bmp");
以下是上述代碼示例的輸出。
下面是在C#中繪制矩形的步驟。
下面的代碼示例顯示了如何在 C# 中的圖像上繪制矩形。
// Create BmpOptions BmpOptions bmpCreateOptions = new BmpOptions(); bmpCreateOptions.BitsPerPixel = 32; // Define the source property for the instance of BmpOptions bmpCreateOptions.Source = new StreamSource(); // Creates an instance of Image and call create method by passing the // bmpCreateOptions object Image image = Image.Create(bmpCreateOptions, 500, 500); // Create and initialize an instance of Graphics class Graphics graphic = new Graphics(image); // Clear the image surface with White color graphic.Clear(Color.White); // Draw a dotted rectangle shape by specifying the Pen object having red // color and a rectangle structure graphic.DrawRectangle(new Pen(Color.Red, 3), new Rectangle(60, 40, 70, 120)); // Draw a continuous rectangle shape by specifying the Pen object having // solid brush with blue color and a rectangle structure graphic.DrawRectangle(new Pen(new SolidBrush(Color.Blue), 3), new Rectangle(40, 60, 120, 70)); // Save all changes image.Save("draw_reactangle.bmp");
以下是上述代碼示例的輸出。
以上便是如何在 C# 中繪制形狀:直線、圓弧、橢圓和矩形,要是您還有其他關于產品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn