Using convert on .NET

ImageMagickObject is a Windows COM+ interface to ImageMagick. COM+, Visual Basic, and Delphi users should post to this discussion group.
Locked
jcasique

Using convert on .NET

Post by jcasique »

Hi, I'm trying to convert TIFF image to JPG using ImageMagickObject (IM 6.4.3 Q16).

Convert from files works ok, but I want to convert a image from one memory stream to othe memory stream to update in a database, i look for the ArrayTest.vbs but it's no clear.

Does anyone has a better example?

Thank you in advance

brainlord

Re: Using convert on .NET

Post by brainlord »

I started a thread on this same topic last week. viewtopic.php?f=8&t=12073:

no real answer yet.

The example uses BLOBs not ADO streams. so it doesn't really apply.

My current workaround is to write the stream to the HD and read it back in. (a waste of time but it works)

Let me know if you figure it out

brainlord

Re: Using convert on .NET

Post by brainlord »

I GOT IT!!

Streams are a newer way to handle binary data than BLOBs.(IM uses BLOBS)
You'll have to convert by READing the stream into the array.

1st dimension the array and stream
dim myArray() as binary, mystream as stream, strPath as string

'read a file as a stream (opr get your stream some other way)
Set myStream = New Stream
strPath="c:\myfolder\myfile"
myStream.Type = adTypeBinary
MyStream.Open
myStream.LoadFromFile (strPath &".jpg")

'... (do whatever to your stream) THEN:

'(redim the array to the size of the stream)
ReDim MyArray(myStream.Size)
'and READ the stream into the array:
myStream.Position = 0
myArray = myStream.Read

'then call the IM COM Object:
strImgMsg = objMagImage.convert("jpg:", myArray, strPath & ".PNG")

'i guess you could also output to another array:
strImgMsg = objMagImage.convert("jpg:", myArray, "png:" myOtherArray)

'then you could write that array to another stream
MyOtherStream.Write MyOtherArray

'NOTE the "jpg:" parameter. that tells IMObject that the following binary array is a jpg. The ArrayTest.VBS example script uses a "null:" parameter but I could never get that to work. (IM has several filetype abbrivs: PICT, PNG, AVI etc)

not sure how you dim the size of the second array though - good luck

HTH (all code is untested)

rvanderkooy

Re: Using convert on .NET

Post by rvanderkooy »

i seem to be having some trouble with the "dim myArray() as binary" part. is that supposed to be byte and not binary? I'm looking for any simple way of working in memory using byte arrays, streams, anything other than disk. any help would be greatly appreciated. thanks in advance.

Alexvb6
Posts: 53
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: Using convert on .NET

Post by Alexvb6 »

Hi,

I have finally found a way to write the ImageMagick output to an in-memory BLOB, and send it directly to the broswer using Response.BinaryWrite, without writing any file on the disk I/O ! This uses Classic ASP, but can easily be ported to .NET, as it is VERY basic !
This is very great for in-memory manipulation (for example generating on-the-fly Captcha).

The solution comes from this page : https://magick-users.imagemagick.narkiv ... ob-to-blob
It cover what you want : read from a Blob (in this case, their example reads the image from a Database).
Here for simplicity, I read the built-in ImageMagick demo Logo.

I hope this helps a lot of you reading this old thread :)

Code: Select all

<%
'This example will write the output to a "Classic ASP 1D Array acting like an in-memory blob", instead of a file, to avoid disk I/O.
'You can then return this "Classic ASP 1D Array acting like an in-memory blob" the browser !


'Declare the 1D Array that will contain the output image data returned by ImageMagick
Dim outputImgData(0)

''Example : Set the Desired Image Format : GIF
'outputImgData(0) = "GIF:"

''Example : Set the Desired Image Format : JPEG
outputImgData(0) = "JPEG:"

'Create IM Instance
Set IM = Server.CreateObject("ImageMagickObject.MagickImage.1")
	
	'Convert the output image ... then do whatever you want with it... then save it to the "Classic ASP 1D Array acting like an in-memory blob" (instead of a file)
	IM.Convert "logo:", "-resize", "300x300", "-flip", outputImgData

'Release IM Instance Resources
Set IM = Nothing
IM = Empty

'Send the output image from the "Classic ASP 1D Array acting like an in-memory blob" to the Browser
'Response.ContentType = "image/gif"
Response.ContentType = "image/jpeg"
Response.BinaryWrite outputImgData
%>

Locked