Popraw przetwarzanie obrazów za pomocą nowoczesnego API

Wprowadzenie

Obecnie biblioteka Aspose.Slides for C++ ma zależności w swoim publicznym API od następujących klas z System::Drawing:

Od wersji 24.4 to publiczne API jest oznaczone jako przestarzałe.

Aby usunąć zależności od System::Drawing w publicznym API, dodaliśmy tak zwane „Modern API”. Metody przyjmujące System::Drawing::Image i System::Drawing::Bitmap są oznaczone jako przestarzałe i powinny być zastąpione odpowiednimi metodami z Modern API. Metody przyjmujące System::Drawing::Graphics są oznaczone jako przestarzałe i nie mają bezpośredniego odpowiednika w Modern API.

W obecnych wersjach traktuj publiczne API zależne od typów System::Drawing jako przestarzałe/legacy. Używaj Modern API dla nowego kodu i przy migracji istniejących przepływów przetwarzania obrazów.

Nowoczesne API

Dodano następujące klasy i wyliczenia do publicznego API:

Użyj GetImage, aby renderować pojedynczy slajd lub kształt. Użyj GetImages, aby renderować kilka slajdów prezentacji. Użyj metod z Images, aby wczytywać obrazy, AddImage z IImage do dodawania ich do prezentacji oraz ReplaceImage z IImage do aktualizacji istniejącego obrazu w prezentacji.

Typowy scenariusz użycia nowego API może wyglądać następująco:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>();
        
// utwórz jednorazowy obiekt IImage z pliku na dysku.  
System::SharedPtr<IImage> image = Images::FromFile(u"image.png");
            
// utwórz obraz PowerPoint, dodając instancję IImage do obrazów prezentacji.
System::SharedPtr<IPPImage> ppImage = pres->get_Images()->AddImage(image);
        
// dodaj kształt obrazu na slajdzie #1
pres->get_Slide(0)->get_Shapes()->AddPictureFrame(Aspose::Slides::ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 100.0f, ppImage);
        
// pobierz instancję IImage reprezentującą slajd #1.
auto slideImage = pres->get_Slide(0)->GetImage(System::Drawing::Size(1920, 1080));

// zapisz obraz na dysku.
slideImage->Save(u"slide1.jpeg", Aspose::Slides::ImageFormat::Jpeg);

Zastępowanie starego kodu przy użyciu Modern API

Aby ułatwić przejście, interfejs nowego IImage powiela oddzielne sygnatury klas System::Drawing::Image i System::Drawing::Bitmap. W praktyce wystarczy zamienić wywołanie starej metody używającej System::Drawing na nową.

Pobieranie miniatury slajdu

Legacy/deprecated API:

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

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

Modern API:

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

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

Pobieranie miniatury kształtu

Legacy/deprecated API:

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

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

Modern API:

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

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

Pobieranie miniatury prezentacji

Legacy/deprecated 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());
}

Modern 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);
}

Dodawanie obrazu do prezentacji

Legacy/deprecated 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);

Modern 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);

Zdezaktualizowane metody/właściwości i ich zamienniki w Modern API

Klasa Presentation

Podpis metody Podpis metody zastępczej
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

Klasa Slide

Podpis metody Podpis metody zastępczej
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

Klasa Shape

Podpis metody Podpis metody zastępczej
GetThumbnail() GetImage()
GetThumbnail(ShapeThumbnailBounds bounds, float scaleX, float scaleY) GetImage(ShapeThumbnailBounds bounds, float scaleX, float scaleY)

Klasa ImageCollection

Podpis metody Podpis metody zastępczej
AddImage(System::SharedPtr<System::Drawing::Image> image) AddImage(System::SharedPtr<IImage> image)

Klasa PPImage

Podpis metody Podpis metody zastępczej
ReplaceImage(System::SharedPtr<System::Drawing::Image> newImage) ReplaceImage(System::SharedPtr<Aspose::Slides::IImage> newImage)
get_SystemImage() get_Image()

Klasa PatternFormat

Podpis metody Podpis metody zastępczej
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)

Klasa IPatternFormatEffectiveData

Podpis metody Podpis metody zastępczej
GetTileImage(System::Drawing::Color background, System::Drawing::Color foreground) GetTileIImage(System::Drawing::Color background, System::Drawing::Color foreground)

Obsługa System::Drawing::Graphics w API

Metody przyjmujące System::Drawing::Graphics są oznaczone jako przestarzałe i nie mają bezpośredniego zamiennika w Modern API.

Użyj metod renderowania obrazu z Modern API zamiast API renderującego do System::Drawing::Graphics:

FAQ

Dlaczego System::Drawing::Graphics został pominięty?

Obsługa System::Drawing::Graphics jest przestarzała w publicznym API, aby ujednolicić pracę z renderowaniem i obrazami, wyeliminować zależności od platformowo‑specyficznych komponentów oraz przejść na podejście wieloplatformowe z IImage. Użyj GetImage lub GetImages zamiast renderowania do System::Drawing::Graphics.

Jakie praktyczne korzyści daje IImage w porównaniu do System::Drawing::Image/System::Drawing::Bitmap?

IImage jednoczy pracę z obrazami rastrowymi i wektorowymi, upraszcza zapisywanie w różnych formatach poprzez ImageFormat, zmniejsza zależność od System::Drawing i sprawia, że kod jest bardziej przenośny między środowiskami.

Czy Modern API wpłynie na wydajność generowania miniatur?

Przejście z GetThumbnail na GetImage nie pogarsza scenariuszy: nowe metody oferują te same możliwości tworzenia obrazów z opcjami i rozmiarami, jednocześnie zachowując wsparcie dla opcji renderowania. Konkretne zyski lub straty zależą od scenariusza, ale funkcjonalnie zamienniki są równoważne.