Giter Club home page Giter Club logo

dxcontrol's Introduction

D2Phap.DXControl

  • A WinForms hybrid control that supports both Direct2D and GDI+ drawing thanks to WicNet.
  • This control has been used in ImageGlass software since version 9.0.

Nuget

Resource links

Features

  • High performance drawing using Direct2D.
  • Names and types are exactly the same as the native concepts of Direct2D (interfaces, enums, structures, constants, methods, arguments, guids, etc...). So you can read the official documentation, use existing C/C++ samples, and start coding with .NET right away.
  • All native COM interfaces are generated as .NET (COM) interfaces, this makes .NET programming easier, but they are not strictly needed.
  • Option to draw by GDI+.
  • Supports animation drawing for both Direct2D and GDI+.

Requirements:

  • .NET 6.0, 7.0, 8.0

Installation

Run the command

Install-Package D2Phap.DXControl

Example

Draws a rectangle, then moves it to the right side.

using D2Phap;

// create a WinForms custom control that extends from DXControl
public class DXCanvas : DXControl
{
    private RectangleF animatableRectangle = new(100, 100, 400, 200);

    public DXCanvas()
    {
        EnableAnimation = true;

        // use Direct2D
        UseHardwareAcceleration = true;
    }

    protected override void OnRender(IGraphics g)
    {
        // draw a yellow rectangle with green border
        g.FillRectangle(rectText, Color.FromArgb(100, Yellow));
        g.DrawRectangle(rectText, Color.Green);
    }


    // Update frame logics for animation
    protected override void OnFrame(FrameEventArgs e)
    {
        // animate the rectangle to the right
        animatableRectangle.left++;
    }
}

See Demo project for full details.

License

MIT

Support this project

Thanks for your gratitude and finance help!

dxcontrol's People

Contributors

d2phap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dxcontrol's Issues

Improve performance for GetPixelColor

https://github.com/d2phap/ImageGlass/blob/e2045cb557300df4260e3374554efa0597bee561/Source/Components/ImageGlass.Base/BHelper/Extensions/ID2D1Bitmap1Extensions.cs#L33-L76

var startIndex = (y * map.pitch) + (x * 4);
var bytes = new byte[4];

Marshal.Copy((nint)(map.bits + startIndex), bytes, 0, bytes.Length); ---------- only read 4 bytes
bitmap1.Unmap();

// since pixel data is D2D1_ALPHA_MODE_PREMULTIPLIED,
// we need to re-calculate the color values
var a = bytes[3];
var alphaPremultiplied = a / 255f;

var r = (byte)(bytes[2] / alphaPremultiplied);
var g = (byte)(bytes[1] / alphaPremultiplied);
var b = (byte)(bytes[0] / alphaPremultiplied);

var color = Color.FromArgb(a, r, g, b);

How to get pixel color?

I want to obtain the current pixel color in the mouse event on the canvas,
How to get pixel color in it? (not from bitmap)

Runtime exception with Nvidia GSYNC enabled

Today I ran into an issue with ImageGlass 9.0 beta 3 (wonderful piece of software, btw) where it crashes a few seconds after startup with the following exception:

Version: ImageGlass v9.0.3.1225
Release code: moon
OS: Microsoft Windows NT 10.0.22621.0 64-bit

----------------------------------------------------
Error:

The wait operation timed out.
----------------------------------------------------

System.ComponentModel.Win32Exception (258): The wait operation timed out.
   at DirectN.VerticalBlankTicker.Wait(Object state) in D:\_GITHUB\@d2phap\DXControl\Source\DXControl\DirectN\DirectWrite\Extensions\VerticalBlankTicker.cs:line 129
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

This happens whether an image file is loaded or not. Clicking Continue in the exception dialog causes the application to crash to desktop. Note that I'm not able to reproduce this issue with ImageGlass 8.8.

After taking a quick look at the source, it seems that the culprit is the DXControl VerticalBlankTicker module waiting for vsync. I have a GSYNC monitor that supports a dynamic refresh rate, which affects how vsync works. Sure enough, the ImageGlass error can be bypassed by creating an application-specific profile for ImageGlass in the NVIDIA Control Panel, and changing the "Monitor Technology" from "GSYNC" to "Fixed Refresh." With this custom configuration, the application works as expected.

I'm running Windows 11 Pro 64-bit Build 22621, DirectX 12, WDDM 3.1, and the NVIDIA driver version 531.26.

I hope this helps. Let me know if I can provide any additional info.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.