رسم تصاویر با استفاده از گرافیک

رسم تصاویر با استفاده از گرافیک

با کتابخانه Aspose.PSD می‌توانید شکل‌های ساده مانند خطوط، مستطیل‌ها و دایره‌ها را رسم کنید، همچنین شکل‌های پیچیده مانند چند‌ضلعی، منحنی‌ها، خم‌ها و شکل‌های بزیر را نیز. کلاس Graphics که در فضای‌نام Aspose.PSD قرار دارد، این اشکال را ایجاد می‌کند. اشیاء گرافیکی مسئول انجام عملیات رسم مختلف روی تصویر هستند و در نتیجه سطح تصویر را تغییر می‌دهند. کلاس Graphics از انواع اشیای کمکی برای بهبود اشکال استفاده می‌کند:

·         Pens، برای رسم خطوط، خطوط حاشیه ای یا بیان شکل‌های هندسی دیگر.

·         Brushes، برای تعریف نحوه پرشدن مناطق.

·         Fonts، برای تعریف شکل حروف متن.

رسم با استفاده از کلاس گرافیک

در زیر، مثال کد نشان داده شده است که استفاده از کلاس Graphics را نشان می‌دهد. کد منبع مثال به چند بخش تقسیم شده است تا ساده و آسان برای دنبال کردن باشد. گام‌به‌گام، مثال‌ها نشان می‌دهند چگونه باید:

  1. تصویری ایجاد کنید.
  2. یک شی از کلاس گرافیک بسازید و مقدماتی کنید.
  3. سطح را پاک کنید.
  4. یک بیضی بکشید.
  5. یک چندضلعی پر شده بکشید و تصویر را ذخیره کنید.

نمونه‌های برنامه‌نویسی

ایجاد یک تصویر

شروع به ایجاد یک تصویر کنید با استفاده از هر یک از روش‌های توصیف شده در ایجاد فایل‌ها.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string loadpath = dataDir + "sample.psd";
string outpath = dataDir + "CoreDrawingFeatures.bmp";
// Create an instance of Image
using (PsdImage image = new PsdImage(loadpath))
{
// load pixels
var pixels = image.LoadArgb32Pixels(new Rectangle(0, 0, 100, 10));
for (int i = 0; i < pixels.Length; i++)
{
// specify pixel color value (gradient in this case).
pixels[i] = i;
}
// save modified pixels.
image.SaveArgb32Pixels(new Rectangle(0, 0, 100, 10), pixels);
// export image to bmp file format.
image.Save(outpath, new BmpOptions());
}

ساخت و مقدماتی کردن یک شی از کلاس گرافیک

سپس یک شی از کلاس گرافیک را با گذراندن شیء Image به سازنده‌اش، ایجاد و مقدماتی کنید.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string outpath = dataDir + "Arc.bmp";
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.BitsPerPixel = 32;
// Create an instance of Image
using (Image image = new PsdImage(100, 100))
{
// Create and initialize an instance of Graphics class and clear Graphics surface
Graphics graphic = new Graphics(image);
graphic.Clear(Color.Yellow);
// Draw an arc shape by specifying the Pen object having red black color and coordinates, height, width, start & end angles
int width = 100;
int height = 200;
int startAngle = 45;
int sweepAngle = 270;
// Draw arc to screen and save all changes.
graphic.DrawArc(new Pen(Color.Black), 0, 0, width, height, startAngle, sweepAngle);
// export image to bmp file format.
image.Save(outpath, saveOptions);
}

پاک کردن سطح

سطح گرافیک را با فراخوانی متد Clear کلاس Graphics پاک کنید و رنگ را به عنوان پارامتر منتقل کنید. این متد سطح گرافیک را با رنگی که به عنوان آرگومان منتقل شده است پر کرده.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Create an instance of Image
using (PsdImage image = new PsdImage(500, 500))
{
var graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.Clear(Color.White);
var pen = new Pen(Color.Blue);
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100));
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f))
{
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) });
}
// export modified image.
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

بکشیدن یک بیضی

ممکن است توجه کنید که کلاس Graphics تعداد بسیاری از متدها برای رسم و پر کردن اشکال اکتشاف کرده است. در مرجع API Aspose.PSD برای .NET لیست کامل متدها را پیدا خواهید کرد. چند نسخه از متد‌های DrawEllipse توسط کلاس گرافیک ارائه شده است. تمام این متدها یک شیء Pen را به عنوان اولین آرگومان خود می‌پذیرند. پارامترهای بعدی برای تعریف مستطیل محاط قایم دور بیضی منتقل می‌شوند. به منظور این مثال، از نسخه‌ای که یک شیء Rectangle را با عنوان دومین پارامتر برای رسم یک بیضی با استفاده از شیء Pen رنگی که مدنظر شماست استفاده کنید.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string loadpath = dataDir + "sample.psd";
string outpath = dataDir + "CoreDrawingFeatures.bmp";
// Create an instance of Image
using (PsdImage image = new PsdImage(loadpath))
{
// load pixels
var pixels = image.LoadArgb32Pixels(new Rectangle(0, 0, 100, 10));
for (int i = 0; i < pixels.Length; i++)
{
// specify pixel color value (gradient in this case).
pixels[i] = i;
}
// save modified pixels.
image.SaveArgb32Pixels(new Rectangle(0, 0, 100, 10), pixels);
// export image to bmp file format.
image.Save(outpath, new BmpOptions());
}

بکشیدن یک چندضلعی پر شده

سپس، یک چندضلعی با استفاده از LinearGradientBrush و یک آرایه از نقاط بکشید. کلاس گرافیک چندین نسخه از متد FillPolygon را بروز کرده است. همه‌ی این‌ها یک شیء Brush را به عنوان اولین آرگومان خود قبول می‌کنند که ویژگی‌های پر کردن را تعریف می‌کند. پارامتر دوم یک آرایه از نقاط است. توجه داشته باشید که هر دو نقطه متوالی در آرایه یک طرف چندضلعی را مشخص می‌کنند.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Create an instance of Image
using (PsdImage image = new PsdImage(500, 500))
{
var graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.Clear(Color.White);
var pen = new Pen(Color.Blue);
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100));
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f))
{
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) });
}
// export modified image.
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

رسم تصاویر با استفاده از گرافیک : منبع کامل

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Create an instance of Image
using (PsdImage image = new PsdImage(500, 500))
{
var graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.Clear(Color.White);
var pen = new Pen(Color.Blue);
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100));
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f))
{
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) });
}
// export modified image.
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

تمام کلاس‌هایی که IDisposable پیاده سازی می‌کنند و به منابع غیرمدیریت شده دسترسی دارند، در یک بیانیه Using نهاده می‌شوند تا اطمینان حاصل شود که به درستی کاهش داده‌اند.