Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting DOC to JPG

39 views
Skip to first unread message

mabosch

unread,
May 9, 2005, 7:56:02 PM5/9/05
to
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

RCS

unread,
May 10, 2005, 12:03:59 AM5/10/05
to
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

"mabosch" <mab...@discussions.microsoft.com> wrote in message
news:76E8D03E-F203-4BED...@microsoft.com...

Message has been deleted

RCS

unread,
May 10, 2005, 9:48:49 AM5/10/05
to
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

"mabosch" <mab...@discussions.microsoft.com> wrote in message

news:301B839E-8CBC-42FD...@microsoft.com...
> 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

mabosch

unread,
May 10, 2005, 10:24:12 AM5/10/05
to
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

0 new messages