Click here to Skip to main content
Click here to Skip to main content

Clipboard ActiveX for Image Copy/Paste into Web Forms

, 11 May 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An article for using Copy/Paste of images on Web forms

Introduction

One of my client requirements was to use Windows copy/paste functionality on Web forms for Images. The client wished to have CTRL+V to paste any image on the client machine or on the clipboard. I just did some Googling and got few modules performing individual tasks. I modified them as per my requirements. The ActiveX gives the following functionalities:

  • Copy Image files from client machine and paste it into our Web forms
  • Upload that image onto our server
  • Use any image content like Microsoft Paint to use cropped part of any image, or Print Screen etc.

Note: My requirement was for using multiple DIVs on forms to use these functionalities, and hence you will find some conditions in JavaScript code in the demo project.

Requirements

As this is a very basic ActiveX control, it has not been signed yet. So in order to use this activeX, you need to Add your site to "Trusted Sites" as well as reduce your browser security levels down so that your browser can use unsigned ActiveX controls for the time being.

You can do this by going to Tools >> Options >> Security >> Trusted Sites.

If you are uploading an image to the server, your Web site requires one folder called Upload with appropriate privileges.

Using the Code

This whole implementation is full use of JavaScripts. I am showing you the important ones here. Form's BODY tag is calling the KeyPress() event, and that function calls the fnCall() function which performs the core operation of Copy/Paste.

Code Description for Demo Project

The HttpUtil1.aspx page is used to upload the pasted image onto the server. postVar is used to pass the query string values to that httpUtil page, id is used to create a sub folder under the Upload folder and will upload a file into it. You can use any generated random number to create different folders each time.

 strURL = strURL + "/HttpUtil1.aspx";
                        postVar = "id=1";

This ActiveX creates temporary files on the client's machine. It only creates files if the user has not copied any existing image (like Paint cut or Print Screen etc). The path where it creates those files can be configured from the code below:

// Getting the Physical path on which the images will be stored on client machine.
var PhysicalPathForTempImage = "c:\\Amit\\";

Below is the call to the ActiveX method to create and save clipboard image onto the client's machine which later can be uploaded onto the server.

var strFilePath = objActiveX.getCopiedImage(PhysicalPathForTempImage);

After pasting the image into DIV, we can upload that onto the server with the same ActiveX. We cannot directly upload the image using any server code or JavaScript since what we have on hand is just fileName, instead of any fileUpload object containing image stream or something like that. So that uploading logic has been shifted into ActiveX logic.

response = objActiveX.UploadFiles(strFileName,strURL,postVar);

Code Description for ActiveX Project

I have used the basic USE32 for clipboard operations and kernel32 for GlobalAlloc, GlobalFree, GlobalLock, etc.

' Clipboard routines.
Private Declare Function OpenClipboard Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "USER32" () As Long
Private Declare Function SetClipboardData Lib "USER32" _
    (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GetClipboardData Lib "USER32" (ByVal wFormat As Long) As Long

' Global memory routines.
Private Declare Function GlobalAlloc Lib "kernel32" _
    (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" _
    (Destination As Any, Source As Any, ByVal Length As Long)

Below is the call to SaveAsJPG module to save the image content into an image file. This saves the file in very compressed size, unlike if we use the SavePicture() method of VB 6.0 which saves files around 2-3 MB in size, while this module saves a file around 60-80 KB of size.

Call SaveAsJPG.SaveJPG(Clipboard.GetData(vbCFBitmap), strTargetFilePath)

In order to upload the pasted image, this ActiveX method sends an HTTP Request to the httpUtil.aspx page to save the image contents to server.

Function UploadFiles(strFileName1 As String, strUrl As String, _
    Optional postVar As String, _
    Optional strUserName As String, Optional strPassword As String) As String

' Set the user name and password.
        WinHttpReq.SetCredentials strUserName, strPassword, _
        HTTPREQUEST_SETCREDENTIALS_FOR_SERVER

Points of Interest

When I started merging different modules into ActiveX, I lost link of the module to save the image in compressed mode, and hence my images were generating with a bulky size. I searched a lot to get that module. Finally, I found it from my Google history which I browsed few months ago. Thanks to Google for keeping my browsed links.

History

  • 11th May, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

AmitChampaneri
Software Developer (Senior) Elan Emerging Technologies Pvt Ltd.
India India
I am a master graduate from Ahmedabad,India.I am in programming since last 2 years,and i love this job of programmer and wanted to do programming for lifetime.

Comments and Discussions

 
QuestionNot working on iss Pinmemberaltanaltan22-May-13 3:54 
AnswerRe: Not working on iss Pinmemberrahulkasar12-Sep-13 4:22 
BugDemo Problem [modified] Pinmembersmurpetx26-Dec-12 21:08 
Questionget the file path PinmemberMember 927009611-Oct-12 20:13 
AnswerRe: get the file path Pinmembersmurpetx23-Jan-13 22:03 
GeneralRe: get the file path PinmemberMember 927009624-Jan-13 12:54 
GeneralRe: get the file path Pinmembersmurpetx24-Jan-13 15:13 
GeneralRe: get the file path PinmemberMember 927009624-Jan-13 16:21 
GeneralRe: get the file path Pinmembersmurpetx24-Jan-13 17:38 
Questiontrouble PinmemberTodor Ivanov Ginchev27-Jul-12 4:56 
QuestionTrying to run the demo PinmemberMember 797599112-Oct-11 8:05 
GeneralUnable to sign the ClipBoard ActiveX PinmemberRafshad3-Sep-10 9:33 
GeneralRe: Unable to sign the ClipBoard ActiveX Pinmemberphylacterion21-Oct-10 12:50 
GeneralRe: Unable to sign the ClipBoard ActiveX PinmemberRafshad11-Nov-10 8:23 
GeneralNot displaying the image Pinmemberphylacterion16-Jun-10 5:36 
GeneralRe: Not displaying the image PinmemberRafshad13-Oct-10 9:19 
GeneralFails on Server Pinmemberraniskummari11-Mar-10 12:04 
GeneralRe: Fails on Server PinmemberTodor Ivanov Ginchev27-Jul-12 4:58 
Generalworks great on my local machine Pinmemberanjali510-Feb-10 10:50 
GeneralRe: works great on my local machine PinmemberAmitChampaneri10-Feb-10 19:17 
GeneralRe: works great on my local machine Pinmemberxgc452-Aug-12 7:13 
GeneralNot working on Windows 7 PinmemberMember 424389812-Jan-10 4:51 
GeneralDemo doesnt work Pinmemberkristapsozols17-Dec-09 4:49 
QuestionDoesn't work on some machines? PinmemberUDG-Mark6-Oct-09 4:33 
AnswerRe: Doesn't work on some machines? PinmemberUDG-Mark6-Oct-09 5:47 
GeneralActiveX dll created from source is not installing Pinmembershrishailn29-May-09 3:41 
GeneralRe: ActiveX dll created from source is not installing PinmemberAmitChampaneri29-May-09 4:13 
GeneralRe: ActiveX dll created from source is not installing Pinmembershrishailn8-Jun-09 0:02 
GeneralclsgTicketUtil.cls Pinmemberthehaunt3-Apr-09 7:47 
GeneralRe: clsgTicketUtil.cls PinmemberAmitChampaneri5-Apr-09 21:15 
GeneralRe: clsgTicketUtil.cls Pinmembermammmel4-May-11 17:48 
QuestionImplement Implement IObjectSafety? PinmemberCraig Schaefer31-Mar-09 11:27 
AnswerRe: Implement Implement IObjectSafety? Pinmemberitfoqus30-Jul-09 0:27 
Generaldifference size in server / client Pinmembercutecat05063-Mar-09 6:52 
GeneralRe: difference size in server / client PinmemberAmitChampaneri3-Mar-09 18:50 
GeneralRe: difference size in server / client Pinmembercutecat05064-Mar-09 3:31 
GeneralRe: difference size in server / client PinmemberMember 1073483110-Apr-14 0:20 
Questiondemo still not fully working PinmemberTara Alvarez14-Jan-09 11:07 
AnswerRe: demo still not fully working PinmemberAmitChampaneri14-Jan-09 19:16 
GeneralRe: demo still not fully working PinmemberTara Alvarez15-Jan-09 1:51 
GeneralRe: demo still not fully working PinmemberAmitChampaneri15-Jan-09 19:09 
GeneralRe: demo still not fully working PinmemberTara Alvarez16-Jan-09 4:45 
GeneralRe: demo still not fully working Pinmembercawoodm5-Feb-09 9:34 
GeneralRe: demo still not fully working PinmemberMember 400910422-Oct-10 20:59 
Generalbasic help [modified] PinmemberTara Alvarez14-Jan-09 10:00 
QuestionNot working on Mozilla PinmemberMurtaza5217-Nov-08 7:29 
AnswerRe: Not working on Mozilla PinmemberAmitChampaneri18-Nov-08 18:32 
AnswerRe: Not working on Mozilla Pinmembercawoodm5-Feb-09 9:14 
GeneralRe: Not working on Mozilla PinmemberAmitChampaneri5-Feb-09 18:52 
AnswerRe: Not working on Mozilla PinmemberMember 424389812-Jan-10 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150331.1 | Last Updated 11 May 2008
Article Copyright 2008 by AmitChampaneri
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid