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:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a