Skip to content

Commit cdbb351

Browse files
committed
updated demo
1 parent 687688c commit cdbb351

File tree

3 files changed

+87
-18
lines changed

3 files changed

+87
-18
lines changed

Source/Demo/DXCanvas.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
using D2Phap;
1+
/*
2+
MIT License
3+
Copyright (C) 2022 DUONG DIEU PHAP
4+
Project & license info: https://github.com/d2phap/DXControl
5+
*/
6+
using D2Phap;
27
using DirectN;
38
using WicNet;
49

510
namespace Demo;
611

712
public class DXCanvas : DXControl
813
{
9-
private ID2D1Bitmap? _d2dBitmap = null;
10-
14+
private IComObject<ID2D1Bitmap>? _d2dBitmap = null;
15+
private D2D_RECT_F rectText = new(100f, 100f, new(400, 200));
16+
1117
public WicBitmapSource? Image
1218
{
1319
set
1420
{
15-
if (DeviceContext == null || value == null)
21+
DXHelper.DisposeD2D1Bitmap(ref _d2dBitmap);
22+
GC.Collect();
23+
24+
if (Device == null || value == null)
1625
{
1726
_d2dBitmap = null;
1827
return;
@@ -31,9 +40,11 @@ public WicBitmapSource? Image
3140
};
3241
var bitmapPropsPtr = bitmapProps.StructureToPtr();
3342

34-
DeviceContext.CreateBitmapFromWicBitmap(value.ComObject.Object, bitmapPropsPtr,
35-
out _d2dBitmap)
43+
Device.CreateBitmapFromWicBitmap(value.ComObject.Object, bitmapPropsPtr,
44+
out ID2D1Bitmap bmp)
3645
.ThrowOnError();
46+
47+
_d2dBitmap = new ComObject<ID2D1Bitmap>(bmp);
3748
}
3849
}
3950

@@ -44,7 +55,7 @@ public DXCanvas()
4455
}
4556

4657

47-
protected override void OnRender(DXGraphics g)
58+
protected override void OnDirect2DRender(DXGraphics g)
4859
{
4960
var p1 = new D2D_POINT_2F(0, 0);
5061
var p2 = new D2D_POINT_2F(ClientSize.Width, ClientSize.Height);
@@ -57,8 +68,8 @@ protected override void OnRender(DXGraphics g)
5768
// draw image
5869
if (_d2dBitmap != null)
5970
{
60-
_d2dBitmap.GetSize(out var size);
61-
g.DrawBitmap(_d2dBitmap,
71+
_d2dBitmap.Object.GetSize(out var size);
72+
g.DrawBitmap(_d2dBitmap.Object,
6273
new D2D_RECT_F(10f, 10f, size.width * 1, size.height * 1),
6374
new D2D_RECT_F(0, 0, size.width, size.height));
6475
}
@@ -95,17 +106,20 @@ protected override void OnRender(DXGraphics g)
95106

96107
}
97108

109+
98110

99-
protected override void OnRender(Graphics g)
111+
protected override void OnGdiPlusRender(Graphics g)
100112
{
101113
using var pen = new Pen(Color.Red, 5);
102114
g.DrawRectangle(pen, new Rectangle(
103115
(int)rectText.left, (int)rectText.top - 50,
104116
(int)rectText.Width, (int)rectText.Height));
117+
118+
g.DrawString($"FPS: {FPS} - GDI+", Font, Brushes.Purple, new PointF(100, 100));
105119
}
106120

107121

108-
private D2D_RECT_F rectText = new(100f, 100f, new(400, 200));
122+
109123
protected override void OnFrame(FrameEventArgs e)
110124
{
111125
base.OnFrame(e);

Source/Demo/Form1.Designer.cs

Lines changed: 30 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Demo/Form1.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using DirectN;
1+
/*
2+
MIT License
3+
Copyright (C) 2022 DUONG DIEU PHAP
4+
Project & license info: https://github.com/d2phap/DXControl
5+
*/
26
using ImageMagick;
37
using Microsoft.Win32.SafeHandles;
4-
using System.Drawing;
58
using System.Reflection;
69
using System.Runtime.InteropServices;
710
using System.Windows.Media.Imaging;
@@ -36,10 +39,36 @@ private void Form1_Load(object sender, EventArgs e)
3639

3740

3841
var obj = Marshal.GetObjectForIUnknown(srcHandle.DangerousGetHandle());
39-
4042
var wicSrc = new WicBitmapSource(obj);
4143
wicSrc.ConvertTo(WicPixelFormat.GUID_WICPixelFormat32bppPBGRA);
4244

4345
return wicSrc;
4446
}
47+
48+
private void canvas_DragDrop(object sender, DragEventArgs e)
49+
{
50+
// Drag file from DESKTOP to APP
51+
if (e.Data is null || !e.Data.GetDataPresent(DataFormats.FileDrop))
52+
return;
53+
54+
var filePaths = (string[])e.Data.GetData(DataFormats.FileDrop, false);
55+
56+
if (filePaths.Length > 0)
57+
{
58+
Text = filePaths[0];
59+
60+
using var imgM = new MagickImage(Text);
61+
canvas.Image = FromBitmapSource(imgM.ToBitmapSource());
62+
}
63+
}
64+
65+
private void canvas_DragOver(object sender, DragEventArgs e)
66+
{
67+
e.Effect = DragDropEffects.Copy;
68+
}
69+
70+
private void chkD2D_CheckedChanged(object sender, EventArgs e)
71+
{
72+
canvas.UseHardwareAcceleration = chkD2D.Checked;
73+
}
4574
}

0 commit comments

Comments
 (0)