Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brush painting from a image #35

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

Zaspard
Copy link

@Zaspard Zaspard commented Sep 28, 2023

About pull request

Added possibility to create a brush by image.

If a brush was created on the image, then:
1)The area is drawn by the image.
2)The area is clipped to the object
3)The object is drawn (if a pen has been set).

Example:
HelloImageWorld-B4FEA3FE6CD74639B3180A99F226EDD7_tempfile.pdf

2023-09-28_11h13_02

Changelog

Added class XImageBrush.cs
For class XImageBrush are realized:

  • CORE
  • WPF (not tested)
  • GDI (not tested)

Added implementation for the XGraphicsPdfRenderer for:

  • DrawEllipse
  • DrawRectangle
  • DrawRoundedRectangle
  • DrawPath
  • DrawPolygon
  • DrawPie (XGraphicsPath.AddPie not implemented)
  • DrawClosedCurve (XGraphicsPath.AddClosedCurve not implemented)

Added a test for the implementation.

-CORE
-GDI (not tested)
-WFP (not tested)
Added test for XImageBrush
@Zaspard
Copy link
Author

Zaspard commented Oct 11, 2023

Should I update to the latest version or close it?

@ThomasHoevel
Copy link
Member

The image looks good, but "WPF (not tested)" and "GDI (not tested)" in the changelog made me hesitate and I did not evaluate the PR yet.
Did you compile and test WPF and GDI meanwhile?

@Zaspard
Copy link
Author

Zaspard commented Oct 16, 2023

I compiled, but didn't test it as I don't know how to test it correctly. I haven't found any tests on WPF and GDI for PDFSharp, only for CORE.
Would it be enough to change the reference from PDFSharp to PDFSharp-WPF in the tests and just run (same for GDI)?

@ThomasHoevel
Copy link
Member

In most cases, the .cs files exist only in the Core folder, but are linked by the GDI and WPF projects.
The implementations are different, but the tests should be the same (except maybe setting the FontResolver for Core tests only).

@Zaspard
Copy link
Author

Zaspard commented Oct 16, 2023

Thanks for the reply, I'll try to get it done this week

…geBrush

Conflicts:
	src/foundation/src/PDFsharp/src/PdfSharp/Drawing.Pdf/XGraphicsPdfRenderer.cs
  -XImageBrush:
    -Added info header to file
    -XImage is always created
  -XGraphicsPdfRenderer:
    -Added XImageBrush implementation when saving to PDF for WPF (tested) and GDI (not tested because NRE in BeginContainer)
@Zaspard
Copy link
Author

Zaspard commented Oct 19, 2023

I fixed the WPF implementation and checked that it works when saving to a PDF file. With GDI, however, there are problems.

In order to clip an area created by an image, I create a container (new XGraphicsContainer), make a SetClip on the passed object (XGraphicsPath), fill the area with images and close the container. In the GDI implementation I need GraphicsState to create a container, but as far as I understand, it may not exist. What should I do in this case (file XGraphicsPdfRenderer.cs)?

                XGraphicsContainer container;
#if GDI
                container = new XGraphicsContainer(_gfx._gfx.Save());
#else
                container = new XGraphicsContainer();
#endif
                BeginContainer(container, xRect.Value, xRect.Value, XGraphicsUnit.Point);
                //I think it should be XCombineMode.Intersect
                SetClip(graphicsPath, XCombineMode.Intersect);
                for (int i = 0; i < xCount; i++)
                {
                    for (int j = 0; j < yCount; j++)
                    {
                        var xPosition = xRect.Value.X + (image.PixelWidth * i);
                        var yPosition = xRect.Value.Y + (image.PixelHeight * j);
                        DrawImage(image, xPosition, yPosition, image.PixelWidth, image.PixelHeight);
                    }
                }
                EndContainer(container);

Another question, as far as I understand in the GDI implementation you can't do this?

            // Create an empty page in this document.
            var page = document.AddPage();

            // Get an XGraphics object for drawing on this page.
            var gfx = XGraphics.FromPdfPage(page);

            var container = gfx.BeginContainer(); // <--throw exception in test

Message: 
-System.InvalidOperationException : _gfx

Stack Trace: 
-XGraphics.BeginContainer(XRect dstrect, XRect srcrect, XGraphicsUnit unit) line 4352
-XGraphics.BeginContainer() line 4293
-ImageTests.Create_brush_by_image() line 261

@Zaspard
Copy link
Author

Zaspard commented Jul 3, 2024

Hello. I have not been able to solve the problem with creating XGraphicsContainer. Can you please tell me if there is any way to create XGraphicsContainer for GDI implementation?

Message: 
-System.InvalidOperationException : _gfx

Stack Trace: 
-XGraphics.BeginContainer(XRect dstrect, XRect srcrect, XGraphicsUnit unit) line 4352
-XGraphics.BeginContainer() line 4293
-ImageTests.Create_brush_by_image() line 261

Best Regards,
Zaspard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants