Is it possible to convert a Windows bitmap with an Alpha channel to a MagickImage? - magick.net

Using Magick.Net, Is it possible to convert (in memory) a Windows bitmap with an Alpha channel to a MagickImage? When I try the following, it fails with .net formats Format32bppPARgb and Format32bppARgb, but works fine with Format24bppRgb. The error message is "no decode delegate for this image format `XWD'".
bmp = New System.Drawing.Bitmap(400, 300, PixelFormat.Format32bppPARgb)
img = New MagickImage(bmp)

You are getting this exception due to a bug in ImageMagick. It reads the stream and tries to determine the format. It incorrectly decides that the format is XWD instead of BMP. I submitted a patch to the GIT repository of ImageMagick to fix this. Your code will work in Magick.NET 7.0.0.0018 that has not been released at the time of writing.

Related

Speech to Text via URL in Visual Studio

I did a lot of research and came up with the code below. It successfully translates speech to text using the microphone.
I have a file on my webserver that streams audio via mp3. It is just a link to an mp3 file. I need to have that translated to text.
I am trying to figure out the best way to do this. So, can you select the audio input as the computers audio (ie play the audio in the web browser)? Or can you stream the audio directly to the translator? I think I need to use SetInputToWaveStream method, but do not understand how to use it.
Private Sub InitializeRecognizerSynthesizer()
Dim selectedRecognizer = ( _
Where e.Culture.Equals(Thread.CurrentThread.CurrentCulture)).FirstOrDefault()
recognizer = New SpeechRecognitionEngine(selectedRecognizer)
recognizer.AudioStateChanged += New EventHandler(Of AudioStateChangedEventArgs)(recognizer_AudioStateChanged)
recognizer.SpeechHypothesized += New EventHandler(Of SpeechHypothesizedEventArgs)(recognizer_SpeechHypothesized)
recognizer.SpeechRecognized += New EventHandler(Of SpeechRecognizedEventArgs)(recognizer_SpeechRecognized)
synthesizer = New SpeechSynthesizer()
End Sub
Private Function SelectInputDevice() As Boolean
Dim proceedLoading As Boolean = True
If IsOscompatible() Then
Try
recognizer.SetInputToDefaultAudioDevice()
Catch
'no audio input device
proceedLoading = False
End Try
Else
ThreadPool.QueueUserWorkItem(InitSpeechRecogniser)
End If
Return proceedLoading
End Function
recognizer.SetInputToWaveFile(file) - will read the audio input from a file in the file system.
recognizer.SetInputToAudioStream - will read the audio input from a stream. A short example:
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
SpeechAudioFormatInfo format = new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono);
recognizer.SetInputToAudioStream(fs, format);
When reading from a stream or a file you must use care to make sure that the audio data is in a supported format. For example, one format I know works on my machine is:
8 bits per sample
single channel mono
22,050 samples per second
PCM encoding
See Help with SAPI v5.1 SpeechRecognitionEngine always gives same wrong result with C# for more info about audio formats.
If your question is how to fetch a resource from a web server and handle it as a stream, see HttpWebResponse.GetResponseStream - http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream(v=vs.100).aspx

Windows Phone 7.1 play recorded PCM/WAV audio

I'm working on a WP7.1 app that records audio and plays it back. I'm using a MedialElement to playback audio. The MediaElement works fine for playing MP4 (actually M4A files renamed) downloaded from the server. However, when I try to play a recorded file with or without the WAV RIFF header (PCM in both cases) it does not work. It gives me an error code 3001, which I cannot find the definition for anywhere.
Can anyone point me to some sample code on playing recorded audio in WP7.1 that does not use the SoundEffect class. Don't want to use the SoundEffect class because it's meant for short audio clips.
This is how I load the audio file:
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (Stream stream = storage.OpenFile(audioSourceUri.ToString(), FileMode.Open))
{
m_mediaElement.SetSource(stream);
}
}
This playing code looks good. Issue have to be in storing code. BTW 3001 means AG_E_INVALID_FILE_FORMAT.
I just realized that the "Average bytes per second" RIFF header value was wrong. I was using the wrong value for the Bits per Sample value, which should've been 16 bit since the microphone records in 16-bit PCM.

Loading large firebird datafile table into a DataSet

I have a Firbird 1.0 data file weighting aprox 25 GB that I am working with it. It has a table which has stored documents and doc's pics as blob. So, I am asking is it possible to open such big data file using fib datasets, i firstly tried to open dataset in runtime = no success as grid was empty so another try was to set it active in design mode which it was also unable to open as it's active property is set to true but no fetched data in grid!
Have you any idea to make it work ? Do I have to set any blob cashe options?
or it is not possible at all?
Now I am developing using my laptop computer (Win 7 x64 4GB Ram ), and later it'll be deployed to my server machine!
I've fixed it!
So another my question is about loading blob data using stream to a TImage component
i am doing like this but it pops out an Access violation
here is my code which you may look at
DM->stImage->Active=true;
try {
TMemoryStream *ms=new TMemoryStream();
TStream *ps=DM->stImage->CreateBlobStream(DM->stImage->FieldByName("PHOTO") ,bmRead);
ms->Position=0;
ms->CopyFrom(ps,ps->Size);
ms->SaveToFile("c:\\1.jpg");
// imgPass->Picture->LoadFromStream(ms);
imgPass->Picture->Graphic->LoadFromStream(ps);
delete ms;
delete ps;
}
catch (Exception &e) {
ShowMessage(e.ToString());
}
it can save it but imgPass->Picture->Graphic->LoadFromStream(ps); does not work!
what could be a problem?
To avoid the AV you need to reset the stream position, that was moved forward during the call to "CopyFrom" function.
So, your code should look like (only the relevant lines):
ms->CopyFrom(ps,ps->Size);
ms->SaveToFile("c:\\1.jpg");
ps->Position = 0; //<<<<<<<<<< here we reset the stream position
imgPass->Picture->Graphic->LoadFromStream(ps);
//imgPass->Picture->Bitmap->LoadFromStream(ps); // <<< if a bitmap and not JPEG
Hope this helps you.
P.S.: this question should be tagged C++ (or C++Builder) because it is not only a database subject.

Converting byte[] of a PDF file original source to MemoryStream for loading into PDF viewer? (component one)

I'm working with a ComponentOne (C1) silverlight PDF viewer control.
It has a "LoadDocument" method that accepts a "Stream".
I'm making an HTTP get call from my client app to get a PDF document.
This document, on the server side, has been streamed in through File.ReadAllBytes(), then converted to a base64 string using Convert.ToBase64String().
This string is sent across the wire back to my silverlight app where it's then reversely converted back into a byte array with Convert.FromBase64String(val).
Then I'm creating a MemoryStream with that byte array and passing "LoadDocument()" that memory stream.
The viewer is rendering nothing. It shows the toolbar and scrollbars, but the contents are blank and the save button is grayed out, suggesting that no document loaded.
I know for certain the file made it across because the byte array size on the client matches teh byte array pre-conversion on the server side.
Here's my code: (in the interest of time/space, i've truncated, removing validation, etc.)
SERVERSIDE
string sendingToClient = Convert.ToBase64String(File.ReadAllBytes(filePath))
CLIENTSIDE
byte[] image = null;
image = Convert.FromBase64String(stringFromServerCall);
MemoryStream stream = new MemoryStream(image);
docViewer.LoadDocument(stream);
edit As a potential workaround, I attempted to save the file into isolated storage with a ".pdf" extension. Then I use the IsolatedStorageFileStream to send to LoadDocument().
I've come to an actual error, it now says "PdfParserException was unhandled by user code: invalid file format (missing pdf header)"
Can anyone shed some light on this PDF header?
Here is an experiment I would conduct.
Add a button to your Xaml and on click use OpenFileDialog to get a FileInfo. From that FileInfo use its Open method to get a stream and pass that to docViewer.LoadDocument.
Now run it, click the button and select the same PDF document you are trying to send from the server.
If that succeeds you need to continue investigating your server streaming strategy. On the other hand if you still have the same problem, well it doesn't get more raw than that. Try other PDF files and start investigating the PDF component. Have you ever actually used it successfully, if so how does this current usage differ.
you should get the stream pointer back to 0 ,so this should do the trick
byte[] image = null;
image = Convert.FromBase64String(stringFromServerCall);
MemoryStream stream = new MemoryStream(image);
stream.Position = 0;
docViewer.LoadDocument(stream);

flex mobile - load image from bytearray - Error #2044: Unhandled securityError

In a mobile application I try to load an image from a sqlite database and want to show it in a mxml image component.
The loading of the bytearray works fine, but when I assign the bytearray to the image component I get following error.
Error #2044: Unhandled securityError:. text=Error #3226: Cannot import
a SWF file when LoaderContext.allowCodeImport is false.
I also tried to save and load the image as a base64 string. but it does not help.
Even if I try a simple thing like this:
var byteArray:ByteArray = img1.loaderInfo.bytes;
img2.source = byteArray;
just add the bytearray of img1 to the empty img2 - the same error occurs.
whats going wrong here?
many thanks for your help,
cheers,
Flo
I don't understand why it thinks you're loading swf bytes.
Try this little hack just for kicks and tell me what happens :
var bitmapData : BitmapData = new BitmapData(content.width,content.height,true,0x00000000);
bitmapData.draw(loader.content, new Matrix(),null,null,null,true);
img2.source = new Bitmap( bitmapData );
where loader is your loader obviously.
Are you using mx or spark Image?
mx:Image extends SWFLoader, so trying to set its source directly could result in a security error because you are essentially importing code. spark:Image wraps a BitmapImage. You will likely have better luck with that.
Edit I just saw your comment. I've also had security issues with loading bytearray data and setting it to the source of BitmapImage. Mine were sandbox issues due to no crossdomain.xml, but I've never used the mobile sdk.

Resources