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

Added BitmapBrush #119

Merged
merged 12 commits into from
Aug 20, 2024
Merged

Added BitmapBrush #119

merged 12 commits into from
Aug 20, 2024

Conversation

beklemeto
Copy link
Contributor

This pull request adds a BitmapBrush. Tested and used successfully for months with v1.4.1. I tried to port the changes to the master branch. Sorry for the untidy pull-request, as it's my first here on github.

Copy link
Collaborator

@drewnoakes drewnoakes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great though @jingwood should sign off.

Would be amazing if you could add an example of using this to one of the demo apps.

Comment on lines +351 to +354
///
/// Extend the edges of the source out by clamping sample points outside the source
/// to the edges.
///
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using <summary> will allow these docs to appear in IDE tooling etc.

Suggested change
///
/// Extend the edges of the source out by clamping sample points outside the source
/// to the edges.
///
/// <summary>
/// Extend the edges of the source out by clamping sample points outside the source
/// to the edges.
/// </summary>

Ditto below.

@@ -129,7 +131,7 @@ inline void SafeRelease(Interface **ppInterfaceToRelease)
}

#define RetrieveContext(ctx) D2DContext* context = reinterpret_cast<D2DContext*>(ctx)
#define RetrieveD2DBitmap(ctx, handle) ID2D1Bitmap* ctx = reinterpret_cast<ID2D1Bitmap*>(handle)
#define RetrieveD2DBitmap(ctx) ID2D1Bitmap* d2dbitmap = reinterpret_cast<ID2D1Bitmap*>(d2dbitmapHandle)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? It seems to rely upon there being a d2dbitmapHandle value in scope, where the previous code (and the #define above) only rely on their "arguments".

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that both arguments, contextHandle and bitmapHandle, should be used. However, RetrieveD2DBitmap is currently not being utilized anywhere. I plan to fix this in the future.

{
public class D2DBitmapBrush : D2DBrush
{
public D2DBitmap Bitmap { get; private set; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be get-only.

Suggested change
public D2DBitmap Bitmap { get; private set; }
public D2DBitmap Bitmap { get; }

@jingwood
Copy link
Owner

Hi @beklemeto! Thanks very much for your work on this.

Could you please address the points that @drewnoakes raised? I'll merge this PR once those issues are resolved.

Additionally, if you could provide an example program, that would be great. It will help us better understand how your feature works.

@jingwood
Copy link
Owner

I'm going to merge this and make a simple example code to test it.

@jingwood jingwood merged commit e92f5a5 into jingwood:master Aug 20, 2024
@beklemeto
Copy link
Contributor Author

I'm going to merge this and make a simple example code to test it.

Thank you for dealing with it. I'm not able to react swiftly.

Maybe it would be useful to point my exact code excerpt on how I used it:

public void DrawPatternPolygon(D2DPoint[] points, Bitmap pattern)
{
	if (graphics != null)
	{
		using (var bmp = panel.Device.CreateBitmapGraphics(pattern.Width, pattern.Height))
		{
			using (var d2dbitmap = panel.Device.LoadBitmap(ImageToByte(pattern)))
			{
				bmp.BeginRender();
				bmp.DrawBitmap(d2dbitmap, new D2DRect(0, 0, pattern.Width, pattern.Height), 1f);
				bmp.EndRender();

				using (var patternBrush = panel.Device.CreateBitmapBrush(bmp.GetBitmap(), D2DExtendMode.Wrap, D2DExtendMode.Wrap, D2DBitmapInterpolationMode.Linear))
				{
					graphics.DrawPolygon(points, D2DColor.Black, 0, D2DDashStyle.Solid, patternBrush);
				}
			}
		}
	}
}

The code repeats a pattern horizontally and vertically (Wrap extend mode) as long as the polygon is fully filled. Please note the double Bitmap conversion through byte array and then drawing onto a bitmap graphics. This was the only way possible to convert transparent GDI Bitmap into a transparent D2DBitmap.

Excellen description on how to use the extend parameters of the brush can be found here:
https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/Direct2D/direct2d-brushes-overview.md

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.

3 participants