Problem with Image Loading (General questions)
How do I get an image to load after decrypting it to a memblob ?
here is where I am right now:
Dim crypt As New wodCryptCom
crypt.LicenseKey = ****-****-****-****
crypt.Optimized = False
Dim in_blob As New MemBlob
Dim out_blob As New MemBlob
crypt.Type = CryptoTypes.AES256
crypt.SecretKey = Key_tb.Text
in_blob.FromFile(curFilePath)
crypt.Decrypt(in_blob, out_blob )
Dim Img As Image
Img = Image.FromStream(out_blob )
PictureBox1.Image = Img
I don't understand why this doesn't work,
any help would be appreciated,
Thanks,
[:wink:]
Re: Problem with Image Loading
Hi Maverick,
I think that I see the problem. Ciphers work on block of data, so when you encrypt, last chunk has to be aligned up to a 'BlockSize' of data, so few bytes are added.
When you decrypt it, those bytes are also added to decrypted file. That's why you see garbage , or like in your case you cannot open the image.
To get rid of it, use Optimized = True, or use PKCS7 padding.
Also I presume that you encrypt your image with wodCrypt.
Hope I helped,
Regards,
Alan
Re: Problem with Image Loading
I tried this but it didn't work, and yes I encrypted it with wodCrypt comp.
crypt.Optimized = True
Dim in_blob As New MemBlob
Dim out_blob As New MemBlob
crypt.Type = CryptoTypes.AES256
crypt.Padding = CryptoPadding.PadPKCS7
crypt.SecretKey = MySecretKey
in_blob.FromFile(System.AppDomain.CurrentDomain.BaseDirectory & Grassy.jpg.enc )
crypt.Decrypt(in_blob, out_blob )
Dim Img As Image
Img = Image.FromStream(out_blob )
PictureBox1.Image = Img
any other ideas ?
Thanks,
Mav
Re: Problem with Image Loading
Maverick,
Did you try encrypt/decrypt any other file then images?
I'll also try this so I can duplicate the problem.
I'll let you know how it goes.
Regards,
Alan
Re: Problem with Image Loading
email me and I'll send you my test app so you don't have to rebuild it, it's very small, and yes it works with text files very well...
Re: Problem with Image Loading
Mav,
what does
Img = Image.FromStream(out_blob )
do? If you make out_blob to be a file, then decrypt to file, and then load it to Img, does it work?
Kreso
Re: Problem with Image Loading
Now I seem to be getting this:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file file & Path because it is being used by another process.
but it is decrypting the file, why can't I use the memory to store and decrypt it instead of saving it to disk ?
Is memoryStream and memBlob not the save, just an area on the computers memory?
Thanks,
Mav
Re: Problem with Image Loading
Mav,
well, yes, MemBlob and MemoryStream *do* the same thing - they keep data in memory. But they *are not* the same, they cannot share data. MemBlob doesn't support IStream interface, so you can't do what you had in mind, at least not directly.
Kreso
Re: Problem with Image Loading
I think the Component is locking the file again, like the problem I had with the email component that you fixed.
Can you fix this one as well ?
Thanks,
Mav
Re: Problem with Image Loading
I think it's intentionaly locking it. Why don't you try to change Filename after encryption is done? That way FileBlob would release the file.
Or use MemBlob and it's Save method instead.
Kreso
Re: Problem with Image Loading
Mav,
not easy. I checked - VB.NET doesn't like my IStream implementation at all, and I'm not sure how to provide IStream interface even if I wanted to.
What I would like to suggest is perhaps use MemBlob, and get bytes out using ToArray. Now you have your own byte array memory you can do whatever you wish with it. Perhaps you could use CreateStreamOnHGlobal API to create IStream on your own memory?
Kreso
Re: Problem with Image Loading
I don't understand, what do I have to do to make this work ?
---Code----
Dim ms As MemoryStream = New MemoryStream(out_blob)
PictureBox1.Image = New Bitmap(ms)
-----------
So I can get the photo into the picturebox without saving to disk.
Your help would be appreciated,
Mav
Re: Problem with Image Loading
Mav,
I know what you mean. Give me a day for this - although this goes (IMO) out of scope our support, I think idea is very good and I'll see how to accomplish it. Someone else may need it too.
Kreso
Re: Problem with Image Loading
Thank You ! very much !
I got it to work, by getting it to delete the photo after.
But it puts it in the recycle bin, which is not good !
Thanks,
Mav
Re: Problem with Image Loading
Mav,
ok, I got an idea. I couldn't personally get it to work, but I hope you'll get lucky :)
Anyway. Use MemBlob as destination. After you decrypt the image to MemBlob, do this:
1. Convert to Byte Array
2. Convert to System.IO.Stream
3. Pass that *YOUR* stream to Image.FromStream method. This fails for me - but I never used this without encryption anyway so I'm not sure how to make it work.
I don't know if it's faster way for conversions, but here's how I did it:
1. Convert to Byte Array [code] Dim d As System.Array
d = out_blob.ToArray()[/code]
2. Convert to System.IO.Stream [code] Dim s As System.IO.Stream
s = New System.IO.MemoryStream(d.Length)
Dim i As Int32
For i = 1 To d.Length
s.WriteByte(d(i))
Next[/code]
3. Pass this to Image.FromStream [code]Img = Image.FromStream(s)[/code]As I said, this doesn't work for me, but perhaps you'll know better.
Hope I helped!
Kreso
Re: Problem with Image Loading
Here is how I did it in VB.NET:
Encypt file on one machine:
--------------------------------------------------------
Public Class Form1
Dim WithEvents crypt1 As WODCRYPTCOMLib.wodCryptCom
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim srcfile As New WODCRYPTCOMLib.FileBlob
Dim destfile As New WODCRYPTCOMLib.FileBlob
crypt1 = New WODCRYPTCOMLib.wodCryptCom
crypt1.Optimized = True
crypt1.Type = WODCRYPTCOMLib.CryptoTypes.AES256
crypt1.SecretKey = weonlydo
srcfile.Filename = z:\image.jpg
destfile.Filename = c:\decimage.jpg
crypt1.Encrypt(srcfile, destfile)
MsgBox( Encyption Complete )
End Sub
End Class
--------------------------------------------------------
Decrypt file to MemBlob on other machine and load picture from MemBlob in PictureBox:
--------------------------------------------------------
Public Class Form1
Dim WithEvents crypt1 As WODCRYPTCOMLib.wodCryptCom
Dim d As System.Array
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim srcfile As New WODCRYPTCOMLib.FileBlob
Dim destmem As New WODCRYPTCOMLib.MemBlob
crypt1 = New WODCRYPTCOMLib.wodCryptCom
crypt1.Type = WODCRYPTCOMLib.CryptoTypes.AES256
crypt1.SecretKey = weonlydo
crypt1.Optimized = True
srcfile.Filename = c:\decimage.jpg
crypt1.Decrypt(srcfile, destmem)
d = destmem.ToArray
MsgBox( Decryption Complete )
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim s As System.IO.Stream
Dim i As Int32
s = New System.IO.MemoryStream(d.Length)
For i = 1 To d.Length
s.WriteByte(d(i))
Next
PictureBox1.Image = Image.FromStream(s)
End Sub
End Class
--------------------------------------------------------
Hope this helps.
Regards,
Drazen
Re: Problem with Image Loading
Thank you so much !
I have been up for 26 hours,
working on some other projects, I took a break from this,
but I will try this as soon as I wake up...
Thanks again! truly!
Mav