이 빠른 자습서에서는 C#에서 그래픽을 그리는 방법에 대해 설명합니다. 프로세스를 훨씬 더 쉽게 설명하기 위해 완전한 단계와 실행 가능한 코드를 제공합니다. 다른 유형의 C# 그리기 모양을 사용하는 동안 JPEG 파일에서 시연되지만 다른 다양한 유형의 이미지도 만들 수 있습니다.
C#에서 그래픽을 그리는 단계
- Nuget 갤러리에서 Aspose.Imaging를 추가하도록 프로젝트 구성
- 출력 JPEG 이미지를 사용자 정의하기 위해 JpegOptions 클래스의 인스턴스를 만듭니다.
- FileCreateSource 개체를 인스턴스화하고 JpegOptions 개체에서 Source로 설정합니다.
- 크기를 제공하여 빈 image 초기화
- 빈 이미지에 대한 Graphics 클래스 개체를 만들고 표면을 지웁니다.
- 다양한 모양을 그리기 위한 펜과 브러시 만들기
- 닫힌 곡선, 직사각형을 그리고 이미지를 JPEG 파일로 저장
다양한 모양과 선의 C# 그리기를 사용하여 이 단계에서 설명합니다. 필요한 라이브러리를 추가한 후 Graphics 클래스 개체와 연결된 빈 이미지를 만들어야 합니다. 이 그래픽 개체에는 샘플 코드에 나와 있는 것처럼 다양한 펜과 브러시를 사용하여 색칠하거나 채울 수 있는 다양한 유형의 모양과 선을 그리는 많은 기능과 속성이 포함되어 있습니다.
C#에서 그래픽을 그리는 코드
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
namespace DrawGraphicsInCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to draw graphics in CSharp | |
{ | |
// Create and instantiate a license to draw graphics in CSharp | |
Aspose.Imaging.License licDrawGraphics= new Aspose.Imaging.License(); | |
licDrawGraphics.SetLicense("Aspose.Imaging.lic"); | |
// Instantiate the JpegOptions and set the color type | |
JpegOptions jpegOptions = new JpegOptions(); | |
jpegOptions.ColorType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode.Rgb; | |
// Set value of JpegOptions.Source to a newly created FileCreateSource class object | |
jpegOptions.Source = new FileCreateSource("SampleImage_out.jpeg", false); | |
using (var jpegImage = Image.Create(jpegOptions, 400, 400)) | |
{ | |
var jpegGraphics = new Graphics(jpegImage); | |
// Clear the image and set its background color | |
jpegGraphics.Clear(Color.LightSkyBlue); | |
// Create a Pen for drawing and set its color | |
var pen = new Pen(Color.DarkCyan); | |
// Draw closed curve by providing list of points | |
jpegGraphics.DrawClosedCurve(pen, new PointF[]{new PointF(50,50),new PointF(150,250),new PointF(350,115),new PointF(75,123),new PointF(23,5)}); | |
// Create a brush to fill the drawing | |
SolidBrush brush = new SolidBrush(Color.Chocolate); | |
// Create filled rectangles using the specified brush | |
jpegGraphics.FillRectangles(brush, new Rectangle[]{new Rectangle(100,100,30,40),new Rectangle(200,200,30,25)}); | |
// Save the resultant image | |
jpegImage.Save(); | |
} | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
*C#의 이 코드는 이미지를 만든 다음 그 위에 다양한 유형의 모양을 그려 그래픽을 그립니다. 여기에서는 JpegOptions를 사용했지만 BmpOptions, GifOptions, PngOptions, SvgOptions 및 TiffOptions와 같은 옵션을 몇 가지 예를 들면 사용할 수 있습니다. 마찬가지로 호, 베지어, 곡선, 닫힌 곡선, 일식, 선, 다각형, 직사각형 등을 그릴 수 있습니다.
이 튜토리얼에서는 이미지에 다양한 유형의 모양을 그리는 방법을 안내했습니다. 이 이미지의 크기를 조정하는 방법을 배우려면 C#에서 이미지 크기를 조정하는 방법에 대한 문서를 참조하세요.