how to create shadow effect using direct2d?

mc 4,516 Reputation points
2024-09-21T00:08:04.9933333+00:00

how to create shadow effect using direct2d to line or rectangle?

I know I can create shadow on ID2D1Image.but Can I create it on line or rectangle?

when I fill rectangle can I create its shaodow? or when I Draw Rectangle can I draw it's shadow?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,634 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,742 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 85,546 Reputation points
    2024-09-21T03:17:19.1866667+00:00

    You can add a Shadow effect on anything you draw .

    For example on a blue Rectangle :

    pDeviceContext->BeginDraw();
    pDeviceContext->Clear(D2D1::ColorF(D2D1::ColorF::White, 1.0f));
    
    D2D1_SIZE_F renderTargetSize = pDeviceContext->GetSize();
    ID2D1BitmapRenderTarget* pCompatibleRenderTarget = NULL;
    HRESULT hr = pDeviceContext->CreateCompatibleRenderTarget(renderTargetSize, &pCompatibleRenderTarget);
    if (SUCCEEDED(hr))
    {
    	pCompatibleRenderTarget->BeginDraw();
    	//pCompatibleRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White, 1.0f));
    	pCompatibleRenderTarget->Clear(NULL);
    	ID2D1SolidColorBrush* pBlackBrush = NULL;
    	pCompatibleRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &pBlackBrush);
    	pCompatibleRenderTarget->FillRectangle(D2D1::RectF(50.f, 50.f, 250.f, 250.f), pBlackBrush);
    	SafeRelease(&pBlackBrush);
    	pCompatibleRenderTarget->EndDraw();
    
    	ID2D1Bitmap* pCompatibleBitmap = NULL;
    	hr = pCompatibleRenderTarget->GetBitmap(&pCompatibleBitmap);
    	if (SUCCEEDED(hr))
    	{
    		ID2D1Effect* pShadowEffect = NULL;
    		hr = pDeviceContext->CreateEffect(CLSID_D2D1Shadow, &pShadowEffect);
    		if (SUCCEEDED(hr))
    		{
    			pShadowEffect->SetValue(D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION, 5.f);
    			pShadowEffect->SetInput(0, pCompatibleBitmap);
    			pDeviceContext->DrawImage(pShadowEffect,
    					D2D1::Point2F(10, 10),
    					D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
    					D2D1_INTERPOLATION_MODE_LINEAR,
    					D2D1_COMPOSITE_MODE_SOURCE_OVER
    				);
    			SafeRelease(&pShadowEffect);
    		}
    		SafeRelease(&pCompatibleBitmap);
    	}
    	SafeRelease(&pCompatibleRenderTarget);
    
    	ID2D1SolidColorBrush* pBlueBrush = NULL;
    	pDeviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Blue), &pBlueBrush);
    	pDeviceContext->FillRectangle(D2D1::RectF(50.f, 50.f, 250.f, 250.f), pBlueBrush);
    	SafeRelease(&pBlueBrush);
    }
    
    pDeviceContext->EndDraw();
    

    User's image

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.