Click here to Skip to main content
6,291,522 members and growing! (14,319 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Intermediate License: The Code Project Open License (CPOL)

Clipboard ActiveX for Image Copy/Paste into Web Forms

By AmitChampaneri

An article for using Copy/Paste of images on Web forms
Javascript, VB 6, Windows, .NET 1.1, .NET 2.0, GDI+, Dev
Posted:11 May 2008
Views:18,171
Bookmarked:11 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 2.96 Rating: 3.80 out of 5

1
2 votes, 33.3%
2

3

4
4 votes, 66.7%
5

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)

About the Author

AmitChampaneri


Member
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.
Occupation: Software Developer (Senior)
Company: Elan Emerging Technologies Pvt Ltd.
Location: India India

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 33 (Total in Forum: 33) (Refresh)FirstPrevNext
GeneralActiveX dll created from source is not installing Pinmembershrishailn3:41 29 May '09  
GeneralRe: ActiveX dll created from source is not installing PinmemberAmitChampaneri4:13 29 May '09  
GeneralRe: ActiveX dll created from source is not installing Pinmembershrishailn0:02 8 Jun '09  
GeneralclsgTicketUtil.cls Pinmemberthehaunt7:47 3 Apr '09  
GeneralRe: clsgTicketUtil.cls PinmemberAmitChampaneri21:15 5 Apr '09  
GeneralImplement Implement IObjectSafety? PinmemberCraig Schaefer11:27 31 Mar '09  
Generaldifference size in server / client Pinmembercutecat05066:52 3 Mar '09  
GeneralRe: difference size in server / client PinmemberAmitChampaneri18:50 3 Mar '09  
GeneralRe: difference size in server / client Pinmembercutecat05063:31 4 Mar '09  
Questiondemo still not fully working PinmemberTara Alvarez11:07 14 Jan '09  
AnswerRe: demo still not fully working PinmemberAmitChampaneri19:16 14 Jan '09  
GeneralRe: demo still not fully working PinmemberTara Alvarez1:51 15 Jan '09  
GeneralRe: demo still not fully working PinmemberAmitChampaneri19:09 15 Jan '09  
GeneralRe: demo still not fully working PinmemberTara Alvarez4:45 16 Jan '09  
GeneralRe: demo still not fully working Pinmembercawoodm9:34 5 Feb '09  
Generalbasic help [modified] PinmemberTara Alvarez10:00 14 Jan '09  
QuestionNot working on Mozilla PinmemberMurtaza527:29 17 Nov '08  
AnswerRe: Not working on Mozilla PinmemberAmitChampaneri18:32 18 Nov '08  
AnswerRe: Not working on Mozilla Pinmembercawoodm9:14 5 Feb '09  
GeneralRe: Not working on Mozilla PinmemberAmitChampaneri18:52 5 Feb '09  
QuestionClassID Pinmemberjbeeler3:56 3 Oct '08  
AnswerRe: ClassID PinmemberAmitChampaneri4:13 3 Oct '08  
GeneralHi Amit, great code. but i have a problem Pinmemberlionsh21:49 26 Aug '08  
QuestionNo image! Only image place holder Pinmemberjbeeler8:07 15 Aug '08  
AnswerRe: No image! Only image place holder PinmemberAmitChampaneri22:32 21 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 May 2008
Editor: Deeksha Shenoy
Copyright 2008 by AmitChampaneri
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project