بهبود پردازش تصویر با API مدرن

مقدمه

در حال حاضر، کتابخانه Aspose.Slides برای C++ وابستگی‌هایی در API عمومی خود به کلاس‌های زیر از System::Drawing دارد:

از نسخه 24.4 به بعد، این API عمومی به‌عنوان منسوخ اعلام شده است.

برای حذف وابستگی‌های API عمومی به System::Drawing، ما به اصطلاح “API مدرن” را اضافه کردیم. متدهایی که از System::Drawing::Image و System::Drawing::Bitmap استفاده می‌کنند به‌عنوان منسوخ اعلام شده و باید با متدهای متناظر API مدرن جایگزین شوند. متدهایی که از System::Drawing::Graphics استفاده می‌کنند نیز منسوخ هستند و جایگزین مستقیم در API مدرن ندارند.

در نسخه‌های جاری، API عمومی که به انواع System::Drawing وابسته است را به‌عنوان قدیمی/منسوخ در نظر بگیرید. برای کدهای جدید و هنگام مهاجرت گردش کارهای پردازش تصویر موجود، از API مدرن استفاده کنید.

API مدرن

کلاس‌ها و enum‌های زیر به API عمومی اضافه شد:

از GetImage برای رندر یک اسلاید یا شکل استفاده کنید. از GetImages برای رندر چندین اسلاید ارائه استفاده کنید. از متدهای Images برای بارگذاری تصاویر، AddImage همراه با IImage برای افزودن آن‌ها به ارائه و ReplaceImage همراه با IImage برای بروزرسانی تصویر موجود در ارائه استفاده کنید.

یک سناریوی معمولی برای استفاده از API جدید به شکل زیر است:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>();
        
// یک نمونه قابل حذف از IImage را از فایل روی دیسک ایجاد کنید.  
System::SharedPtr<IImage> image = Images::FromFile(u"image.png");
            
// یک تصویر PowerPoint ایجاد کنید با افزودن یک نمونه IImage به تصاویر ارائه.
System::SharedPtr<IPPImage> ppImage = pres->get_Images()->AddImage(image);
        
// یک شکل تصویر را بر روی اسلاید #1 اضافه کنید
pres->get_Slide(0)->get_Shapes()->AddPictureFrame(Aspose::Slides::ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 100.0f, ppImage);
        
// یک نمونه از IImage که اسلاید #1 را نشان می‌دهد دریافت کنید.
auto slideImage = pres->get_Slide(0)->GetImage(System::Drawing::Size(1920, 1080));

// تصویر را روی دیسک ذخیره کنید.
slideImage->Save(u"slide1.jpeg", Aspose::Slides::ImageFormat::Jpeg);

جایگزینی کد قدیمی با API مدرن

برای تسهیل انتقال، رابط جدید IImage امضای جداگانهٔ کلاس‌های System::Drawing::Image و System::Drawing::Bitmap را تکرار می‌کند. به‌طور کلی، تنها کافیست فراخوانی متد قدیمی که از System::Drawing استفاده می‌کند را با متد جدید جایگزین کنید.

دریافت تصویر بندانگشتی اسلاید

API کهنه/منسوخ:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");

pres->get_Slide(0)->GetThumbnail()->Save(u"slide1.png");

API مدرن:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");

pres->get_Slide(0)->GetImage()->Save(u"slide1.png");

دریافت تصویر بندانگشتی شکل

API کهنه/منسوخ:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");

pres->get_Slide(0)->get_Shape(0)->GetThumbnail()->Save(u"shape.png");

API مدرن:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");

pres->get_Slide(0)->get_Shape(0)->GetImage()->Save(u"shape.png");

دریافت تصویر بندانگشتی ارائه

API کهنه/منسوخ:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");

auto bitmaps = pres->GetThumbnails(System::MakeObject<RenderingOptions>(), System::Drawing::Size(1980, 1028));

for (int32_t index = 0; index < bitmaps->get_Length(); index++)
{
    System::SharedPtr<System::Drawing::Bitmap> thumbnail = bitmaps[index];
    thumbnail->Save(System::String::Format(u"slide_{0}.png", index), System::Drawing::Imaging::ImageFormat::get_Png());
}

API مدرن:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");

auto images = pres->GetImages(System::MakeObject<RenderingOptions>(), System::Drawing::Size(1980, 1028));

for (int32_t index = 0; index < images->get_Length(); index++)
{
    System::SharedPtr<IImage> thumbnail = images[index];
    thumbnail->Save(System::String::Format(u"slide_{0}.png", index), Aspose::Slides::ImageFormat::Png);
}

افزودن تصویر به ارائه

API کهنه/منسوخ:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

System::SharedPtr<System::Drawing::Image> image = System::Drawing::Image::FromFile(u"image.png");

System::SharedPtr<IPPImage> ppImage = pres->get_Images()->AddImage(image);

pres->get_Slide(0)->get_Shapes()->AddPictureFrame(Aspose::Slides::ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 100.0f, ppImage);

API مدرن:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

System::SharedPtr<Aspose::Slides::IImage> image = Aspose::Slides::Images::FromFile(u"image.png");

System::SharedPtr<IPPImage> ppImage = pres->get_Images()->AddImage(image);

pres->get_Slide(0)->get_Shapes()->AddPictureFrame(Aspose::Slides::ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 100.0f, ppImage);

متدها/ویژگی‌های منسوخ و جایگزین‌های آن‌ها در API مدرن

کلاس Presentation

امضای متد امضای متد جایگزین
GetThumbnails(System::SharedPtr<Export::IRenderingOptions> options) GetImages(System::SharedPtr<Export::IRenderingOptions> options)
GetThumbnails(System::SharedPtr<Export::IRenderingOptions> options, System::ArrayPtr<int32_t> slides) GetImages(System::SharedPtr<Export::IRenderingOptions> options, System::ArrayPtr<int32_t> slides)
GetThumbnails(System::SharedPtr<Export::IRenderingOptions> options, float scaleX, float scaleY) GetImages(System::SharedPtr<Export::IRenderingOptions> options, float scaleX, float scaleY)
GetThumbnails(System::SharedPtr<Export::IRenderingOptions> options, System::ArrayPtr<int32_t> slides, float scaleX, float scaleY) GetImages(System::SharedPtr<Export::IRenderingOptions> options, System::ArrayPtr<int32_t> slides, float scaleX, float scaleY)
GetThumbnails(System::SharedPtr<Export::IRenderingOptions> options, System::Drawing::Size imageSize) GetImages(System::SharedPtr<Export::IRenderingOptions> options, System::Drawing::Size imageSize)
GetThumbnails(System::SharedPtr<Export::IRenderingOptions> options, System::ArrayPtr<int32_t> slides, System::Drawing::Size imageSize) GetImages(System::SharedPtr<Export::IRenderingOptions> options, System::ArrayPtr<int32_t> slides, System::Drawing::Size imageSize)
Save(System::String fname, System::ArrayPtr<int32_t> slides, Export::SaveFormat format) No Modern API replacement
Save(System::String fname, System::ArrayPtr<int32_t> slides, Export::SaveFormat format, System::SharedPtr<Export::ISaveOptions> options) No Modern API replacement

کلاس Slide

امضای متد امضای متد جایگزین
GetThumbnail() GetImage()
GetThumbnail(float scaleX, float scaleY) GetImage(float scaleX, float scaleY)
GetThumbnail(System::Drawing::Size imageSize) GetImage(System::Drawing::Size imageSize)
GetThumbnail(System::SharedPtr<Export::ITiffOptions> options) GetImage(System::SharedPtr<Export::IRenderingOptions> options
GetThumbnail(System::SharedPtr<Export::IRenderingOptions> options) GetImage(System::SharedPtr<Export::IRenderingOptions> options)
GetThumbnail(System::SharedPtr<Export::IRenderingOptions> options, float scaleX, float scaleY) GetImage(System::SharedPtr<Export::IRenderingOptions> options, float scaleX, float scaleY)
GetThumbnail(System::SharedPtr<Export::IRenderingOptions> options, System::Drawing::Size imageSize) GetImage(System::SharedPtr<Export::IRenderingOptions> options, System::Drawing::Size imageSize)
RenderToGraphics(System::SharedPtr<Export::IRenderingOptions> options, System::SharedPtr<System::Drawing::Graphics> graphics) No Modern API replacement
RenderToGraphics(System::SharedPtr<Export::IRenderingOptions> options, System::SharedPtr<System::Drawing::Graphics> graphics, float scaleX, float scaleY) No Modern API replacement
RenderToGraphics(System::SharedPtr<Export::IRenderingOptions> options, System::SharedPtr<System::Drawing::Graphics> graphics, System::Drawing::Size renderingSize) No Modern API replacement

کلاس Shape

امضای متد امضای متد جایگزین
GetThumbnail() GetImage()
GetThumbnail(ShapeThumbnailBounds bounds, float scaleX, float scaleY) GetImage(ShapeThumbnailBounds bounds, float scaleX, float scaleY)

کلاس ImageCollection

امضای متد امضای متد جایگزین
AddImage(System::SharedPtr<System::Drawing::Image> image) AddImage(System::SharedPtr<IImage> image)

کلاس PPImage

امضای متد امضای متد جایگزین
ReplaceImage(System::SharedPtr<System::Drawing::Image> newImage) ReplaceImage(System::SharedPtr<Aspose::Slides::IImage> newImage)
get_SystemImage() get_Image()

کلاس PatternFormat

امضای متد امضای متد جایگزین
GetTileImage(System::Drawing::Color background, System::Drawing::Color foreground) GetTile(System::Drawing::Color background, System::Drawing::Color foreground)
GetTileImage(System::Drawing::Color styleColor) GetTile(System::Drawing::Color styleColor)

کلاس IPatternFormatEffectiveData

امضای متد امضای متد جایگزین
GetTileImage(System::Drawing::Color background, System::Drawing::Color foreground) GetTileIImage(System::Drawing::Color background, System::Drawing::Color foreground)

پشتیبانی API برای System::Drawing::Graphics

متدهایی که از System::Drawing::Graphics استفاده می‌کنند به‌عنوان منسوخ اعلام شده و جایگزین مستقیم در API مدرن ندارند.

به‌جای API که به System::Drawing::Graphics رندر می‌کند، از متدهای رندر تصویر API مدرن استفاده کنید:

سؤالات متداول

چرا System::Drawing::Graphics حذف شد؟

پشتیبانی از System::Drawing::Graphics در API عمومی منسوخ شده است تا کار با رندر و تصاویر یکپارچه شود، وابستگی‌های خاص پلتفرم حذف شوند و به رویکردی چندپلتفرمی با استفاده از IImage سوئیچ شود. به‌جای رندر به System::Drawing::Graphics از GetImage یا GetImages استفاده کنید.

مزیت عملی استفاده از IImage نسبت به System::Drawing::Image/System::Drawing::Bitmap چیست؟

IImage کار با تصاویر رستر و برداری را یکپارچه می‌کند، ذخیره‌سازی به قالب‌های مختلف را از طریق ImageFormat ساده می‌سازد، وابستگی به System::Drawing را کاهش می‌دهد و کد را بین محیط‌ها قابل حمل می‌کند.

آیا API مدرن بر عملکرد تولید تصویرهای بندانگشتی تأثیر خواهد گذاشت؟

جایگزینی GetThumbnail با GetImage عملکرد را بدتر نمی‌کند: متدهای جدید همان قابلیت تولید تصویر با گزینه‌ها و اندازه‌ها را دارند و همچنان از گزینه‌های رندر پشتیبانی می‌کنند. سود یا کاهش خاص بسته به سناریو متفاوت است، اما از نظر عملکردی جایگزین‌ها برابرند.