Pages

Wednesday, December 1, 2010

Lotusscript code to get the mime content of an image

Function  getMimeofImage( imagePath As String,targetFilePath As String) As Boolean
'************************************************************************************************************ 
'@Purpose     :   To create mime equivalent of an image file in a text format and export the same to a target file
'************************************************************************************************************

'mark the flow of control moving inside the current function
getMimeofImage=False

'declare variables and objects necessary for further manipulation
Dim session As New notessession
Dim currDB As NotesDatabase
Dim tempDoc As NotesDocument
Dim stream As NotesStream
Dim mimeEntity As NotesMimeEntity
Dim imageSize As Long

'initiation
'handler errors in case of abrupt termination
On Error Goto mimeErrHandler

'set handle for the current database
Set currDb=session.CurrentDatabase
'create a temporary document
Set tempDoc=currDb.CreateDocument

'create a stream object
Set stream = session.CreateStream
If Not stream.Open(imagePath) Then
Error 1404, "Cannot open file " & imagePath & " for processing."
Exit Function
End If

imageSize = stream.Bytes

Set tempDoc=currDB.CreateDocument
Set mimeEntity = tempDoc.CreateMIMEEntity
If Right(Lcase(imagePath), 4) = ".gif" Then
Call mimeEntity.SetContentFromBytes(stream, "image/gif", ENC_NONE)
Else
Call mimeEntity.SetContentFromBytes(stream, "image/jpeg", ENC_NONE)
End If
Call mimeEntity.EncodeContent(ENC_BASE64)

Call stream.Close


Set stream=Nothing
Set stream=session.CreateStream
Call stream.Open(targetFilePath)
If stream.Bytes <> 0 Then
Call stream.Truncate
End If
' Call stream.WriteText()

Call stream.WriteText(|<?xml version='1.0'?><document xmlns='http://www.lotus.com/dxl' version='8.5' maintenanceversion='1.0' replicaid='652577AF00410F29' form='dxlManipulations'><noteinfo noteid='94e' unid='8F605FA904982F43652577EB00207375' sequence='1'><created><datetime>20101130T112427,09+0530</datetime></created><modified><datetime>20101130T112427,10+0530</datetime></modified><revised><datetime>20101130T112427,09+0530</datetime></revised><lastaccessed><datetime>20101130T112427,09+0530</datetime></lastaccessed><addedtofile><datetime>20101130T112427,09+0530</datetime></addedtofile></noteinfo><updatedby><name>CN=Karthikeyan A/O=Maargasystems</name></updatedby><item name='rt'><richtext><pardef id='1'/><par def='1'><picture width='183px' height='62px'><gif>|+mimeEntity.ContentAsText+|</gif></picture></par></richtext></item><item name='TestField'><text>3</text></item></document>|)
' Call stream.WriteText()

Call stream.Close

'mark the flow of control moving out of the current function
getMimeofImage=True
Exit Function
mimeErrHandler:
Print "Error ***" & Error & "*** occured on line ***" & Cstr(Erl) & "*** with error number ***"   _
& Cstr(Err) & "*** in Method ""CreateMIMEEntity"" "
Exit Function
End Function

Hope this helps :)

No comments:

Post a Comment