Copy Drawn Picture to Clipboard

Copy Drawn Picture to Clipboard

The VB Picture control can hold several different formats of pictures: BMP, DIB, ICO, CUR, WMF, and others under VB5. Additionally, you can use graphics methods to “draw” on the control. The only native method that converts the image on the picture control, including the drawn graphics, to a bitmap and transfers the bitmap to the system clipboard requires you to use AutoRedraw.

However, this technique causes problems. This code shows the declarations and functions required to transfer the image on a VB picture control to the system clipboard as a bitmap. Add this code to a BAS module, call PicToClip, and pass the picture box as the only parameter:

 '	#'	# API Declarations'	#'	BitmapPrivate Declare Function BitBlt Lib "gdi32" _	(ByVal hDestDC As Long, ByVal x As Long, _	ByVal y As Long, ByVal nWidth As Long, _	ByVal nHeight As Long, ByVal hSrcDC As _	Long, ByVal xSrc As Long, ByVal ySrc As _	Long, ByVal dwRop As Long) As LongPrivate Declare Function _	CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, _	ByVal nWidth As Long, ByVal nHeight As Long) As LongPrivate Declare Function CreateCompatibleDC _	Lib "gdi32" (ByVal hDC As Long) As LongPrivate Declare Function DeleteDC Lib _	"gdi32" (ByVal hDC As Long) As LongPrivate Declare Function GetDC Lib "user32" _	(ByVal hWnd As Long) As LongPrivate Declare Function ReleaseDC Lib "user32" _	(ByVal hWnd As Long, ByVal hDC As Long) As LongPrivate Declare Function SelectObject Lib "gdi32" _	(ByVal hDC As Long, ByVal hObject As Long) As Long' ClipboardPrivate Declare Function OpenClipboard Lib _	"user32" (ByVal hWnd As Long) As LongPrivate Declare Function CloseClipboard Lib _	"user32" () As LongPrivate Declare Function EmptyClipboard Lib _	"user32" () As LongPrivate Declare Function SetClipboardData Lib "user32" _	(ByVal wFormat As Long, ByVal hMem As Long) As Long'	#'	# API Constants'	#'	Clipboard formatsPrivate Const CF_BITMAP = 2' ROPPrivate Const SRCCOPY = &HCC0020Public Sub PicToClip(pic As PictureBox)	Dim hSourceDC As Long	Dim hMemoryDC As Long	Dim lWidth As Long	Dim lHeight As Long	Dim hBitmap As Long	Dim hOldBitmap As Long	'	#	'	# NOTE: Error trapping has been removed 	'	for the sake of clarity	'	#	With pic		' Determine bitmap size		lWidth = .Parent.ScaleX(.ScaleWidth, _			.ScaleMode, vbPixels)		lHeight = .Parent.ScaleY(.ScaleHeight, _			.ScaleMode, vbPixels)		' Get hBitmap loaded with image on 		' Picture control		hSourceDC = GetDC(.hWnd)		hMemoryDC = CreateCompatibleDC(.hDC)		hBitmap = CreateCompatibleBitmap( _			.hDC, lWidth, lHeight)		hOldBitmap = SelectObject(hMemoryDC, _			hBitmap)		Call BitBlt(hMemoryDC, 0, 0, lWidth, _			lHeight, pic.hDC, 0, 0, SRCCOPY)		hBitmap = SelectObject(hMemoryDC, _			hOldBitmap)		' Copy to clip board		Call OpenClipboard(.Parent.hWnd)		Call EmptyClipboard		Call SetClipboardData(CF_BITMAP, _			hBitmap)		Call CloseClipboard		' Clean up GDI		Call ReleaseDC(.hWnd, hSourceDC)		Call SelectObject(hMemoryDC, hBitmap)		Call DeleteDC(hMemoryDC)	End WithEnd Sub
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as