Converting DOC to JPG

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to convert a DOC to JPG it works in Windows Forms, but I run under
asp.net the clipboard its empty, I'm doing

Dim objWord As New Word.Application
Dim objDoc As Word.Document
Const CF_ENHMETAFILE As Integer = 14
objDoc = objWord.Documents.Open("C:\TEST\Test1.doc")
objWord.ActiveDocument.Select()
objWord.Selection.CopyAsPicture()
Dim ip As IntPtr
Dim metaFile As System.Drawing.Imaging.Metafile
Dim bRet As Boolean
bRet = ClipboardAPI.OpenClipboard(Me.Handle)
If bRet = True Then
'Verify the clipboard contains data available
'as an enhanced metafile.
bRet = _
ClipboardAPI.IsClipboardFormatAvailable(CF_ENHMETAFILE)
<> 0
End If

If bRet = True Then
'Store the clipboard's contents in the IntPtr.
ip = ClipboardAPI.GetClipboardData(CF_ENHMETAFILE)
End If

'Verify the IntPrt contains data before proceeding. Passing
'an empty IntPtr to System.Drawing.Imaging.Metafile results
'in an exception.
If Not IntPtr.Zero.Equals(ip) Then
metaFile = New System.Drawing.Imaging.Metafile(ip, True)
ClipboardAPI.CloseClipboard()
Dim image As System.Drawing.Image = metaFile
'Me.PictureBox1.Image = metaFile

Dim objImageWriter As Image = New Bitmap(image.Width,
image.Height)

Dim objGraphics As Graphics =
Graphics.FromImage(objImageWriter)

objGraphics.Clear(Color.White)
'objGraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
objGraphics.DrawImage(image, 0, 0, image.Width, image.Height)


image.Dispose()
objGraphics.Dispose()

Dim ep As EncoderParameters = New EncoderParameters
ep.Param(0) = New
System.drawing.imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100)

Dim codecs() As ImageCodecInfo =
ImageCodecInfo.GetImageEncoders()
Dim iciInfo As ImageCodecInfo
Dim item As ImageCodecInfo

For Each item In codecs
If (item.MimeType = "image/jpeg") Then iciInfo = item
Next

objImageWriter.Save("c:\test\test1.jpg", iciInfo, ep)
objImageWriter.Dispose()


End If




Public Class ClipboardAPI

<DllImport("user32.dll", EntryPoint:="OpenClipboard", _
SetLastError:=True, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function OpenClipboard(ByVal hWnd As IntPtr) As Boolean
End Function

<DllImport("user32.dll", EntryPoint:="EmptyClipboard", _
SetLastError:=True, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EmptyClipboard() As Boolean
End Function

<DllImport("user32.dll", EntryPoint:="SetClipboardData", _
SetLastError:=True, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> Public Shared Function
SetClipboardData(ByVal uFormat As Integer, ByVal ByValhWnd As IntPtr) As
IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="CloseClipboard", _
SetLastError:=True, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function CloseClipboard() As Boolean
End Function

<DllImport("user32.dll", EntryPoint:="GetClipboardData", _
SetLastError:=True, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetClipboardData(ByVal uFormat As Integer) As
IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="IsClipboardFormatAvailable", _
SetLastError:=True, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function IsClipboardFormatAvailable(ByVal uFormat As
Integer) As Short
End Function
End Class
 
First thing that comes to mind, is that your ASP.NET app is running as
aspnet (the user account), which is a limited account. And I want to say it
doesn't have "login locally" priviledge, and even further - even if it did,
it's running non-interactively (as a process that doesn't have a windows
context) - and I *believe* the clipboard is a function of the Windows shell
(read: explorer.exe, advapi32.dll, etc, etc)

My guess would be you aren't going to be able to use the clipboard
non-interactively.. HTH
 
Thanks for your answer, I Have in the web.config impersonate =true with user
and Password.

can I force UIPermissionClipboard.AllClipboard , I really don't know how to
use it in VB

Thank you again

Marcelo
 
Again, I'm not positive, I just think that you aren't going to be able to
use UI elements from a non-UI process. Kind of like if you tried to get the
current window handle to pop up a MessageBox, from ASP.NET - you would find
you get a null handle.

The clipboard, to my knowledge, is an in-process function that is made
available via explorer.exe and the Shell32 extensions (i.e. the Windows
shell, the Start button, desktop, taskbar, etc) - and those require a
windows context, which you won't be able to get, from a webapp.

Good luck
 
Thank again

I Discover that the data is in the clipboard, because the following works in
asp.net

.......
objWord.Selection.CopyAsPicture()
objWord.Documents.Add()
objWord.Documents.Item(2).Activate()
objWord.ActiveDocument.Select()
objWord.Selection.Paste()
objWord.ActiveDocument.SaveAs("c:\test\MyNewDocument.doc")

Copy & Paste works inside the Word, the problem is that I can't read it from
my page, I'm thinking to use another application to paste and save (but I
can't find it because the paint is not a COM and when I paste (manually) in
Photodraw only paste text, It loose the format).

What doyou suggest ?

Regards
Marcelo
 
Back
Top