473,748 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to decode a JPEG 2000 file or use any internal decoding of WebResponse

In my need to decode a JPEG 2000 file, I discovered like many that
there was no functionality for this in the .NET Framework. Instead of
forking out a pile of cash to do this, I came up with the idea that
costs nothing and it is inheritently built into the Framework. So here
is the solution...

When you use the WebRequest and WebResponse classes to obtain graphics
from a web site, these classes have built-in decoding for JPEG 2000
files. Now before we go any further, this article is not about
downloading and decoding files over the Internet. It is about decoding
JPEG 2000 files locally or using any of the internal decoding
mechanisms that are part of WebResponse and probably Internet Explorer
for that matter. Too bad Microsoft didn't expose the underlying APIs
for decoding. But being the code thieves that we all are, we look for
other ways to get in.

The concept is very simple. It consists of the following steps:

1. Set up a mini web server in your code using TcpListener.
2. Create a HTTP request to the server.
3. Use WebRequest and WebResponse to call your web server.
4. Return the encoded JPEG file from the web server.
5. Your done.

Now for a few specifics. When you setup a mini web server, you use the
address 127.0.0.1 and any free port.

You then use WebRequest to call http://127.0.0.1:54321 (or whatever
port you are using).

In your web server, you simply return a response with a 200 code with
a few headers followed by the actual encoded JPEG.

Your web server must remain running until the GetResponseStre am that
you use to retrieve the data has completed. If the web server
terminates, you will fail to read all of the data. Only after you have
retrieved the data should you shutdown the web server.

Here is some coding example. Note: this code was partially copied and
pasted from my own code, so it may not work. Use it only to give you
an idea what needs to be done. Take care that the newsgroup wraps the
text, so you need to unwrap it:

using System;
using System.Windows. Forms;
using System.Text;
using System.IO;
using System.Net.Sock ets;
using System.Net;
using System.Threadin g;
using System.Collecti ons;

private bool webServerStarte d = false;
private string encodedJPEGFile ;
private bool finishedRetriev ing = false
..
..
..

// Read in your encoded JPEG 2000 file or get it from somewhere else
such as a stream.
// You must use the 1252 encoding to make sure that the binary data is
read correctly as string data.
StreamReader stream = new StreamReader(@" C:\temp\Encoded File.jpg",
Encoding.GetEnc oding(1252));
string picture = stream.ReadToEn d();
stream.Close();

// Setup a thread for your web server
StringBuilder data = new StringBuilder() ;
this.webServerS tarted = false;
Thread webServerThread = new Thread(new ThreadStart(Web Server));
webServerThread .Start();

// Wait until the thread has started.
while(!this.web ServerStarted)
{
Application.DoE vents();
}

// Request the file.
WebRequest WReq = WebRequest.Crea te("http://127.0.0.1:54321 ");
// The GetResponse takes care of all conversions
WebResponse WResp = WReq.GetRespons e();

Stream responseStream = WResp.GetRespon seStream();

while(true)
{
int bytesRead = responseStream. Read(buffer, 0, buffer.Length);

if (bytesRead == 0)
break;

string packet = Encoding.GetEnc oding(1252).Get String(buffer, 0,
bytesRead);
data.Append(pac ket);
}

// Tell your web server that you are done.
this.finishedRe trieving = true;

// Here is your decoded picture - Smile!
pictureData = data.ToString() ;

public void WebServer()
{
try
{
int bytesSent;

// Create the http response. Note the double CrLf prior to
appending your jpeg data.
string http =
"HTTP/1.1 200 OK\r\n" +
"Content-Type: image/jpeg\r\n" +
"Connection : Keep-Alive\r\n\r\n" +
this.encodedJPE GFile;

// 16777343 is the equivalent of the address 127.0.0.1. Note,
localhost does not always work, so use 127.0.0.1
IPAddress localAddress = new IPAddress(16777 343);

TcpListener listener = new TcpListener(loc alAddress, 59501);
listener.Start( );

// Tell the calling client that the server is now running.
this.webServerS tarted = true;

Socket socket = listener.Accept Socket();

if (socket.Connect ed)
{
// Write the response to the browser.
byte[] bufferOut = Encoding.GetEnc oding(1252).Get Bytes(http);

int bytesSent = socket.Send(buf ferOut, bufferOut.Lengt h, 0);

// Wait until the caller has received the data.
while(!this.fin ishedRetrieving )
{
Application.DoE vents();
}

socket.Shutdown (SocketShutdown .Both);
socket.Close();
}
}
catch(Exception ex)
{
string message = ex.Message;
}
}

Hope it Helps!
Johann Blake
Nov 16 '05 #1
0 2763

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1330
by: BRAINIAC | last post by:
I am trying to process JPEG images using PIL 1.1.4 with Python 2.2.3. GIF and PNG are working fine. I am using jpeg-6b and all my other software using libjpeg work fine. I am thinking you need much more info to help me, but let me start with this: Python 2.2.3 (#2, Feb 3 2004, 03:09:11) on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> import Image >>> a=Image.open("lena.jpg")
1
21567
by: AR | last post by:
I would like to know more about the Encode/Decode feature available within MS Access. This is what I have read from Microsoft Office OnLine: "The simplest method of protection is to encode the database. Encoding a database compacts the database file and helps protect it from being read by a word processor." Is this recommended as a best practice? What is the impact on
4
9646
by: Laszlo Szijarto | last post by:
anyone know of a JPEG 2000 encoding / decoding library that works with .NET? Thank you, Laszlo
2
10318
by: aurora | last post by:
I have some unicode string with some characters encode using python notation like '\n' for LF. I need to convert that to the actual LF character. There is a 'unicode_escape' codec that seems to suit my purpose. >>> encoded = u'A\\nA' >>> decoded = encoded.decode('unicode_escape') >>> print len(decoded) 3 Note that both encoded and decoded are unicode string. I'm trying to use
2
3425
by: rsd | last post by:
Hi, I'm trying get Samsung YH-920 mp3 player to work with Debian GNU/Linux. To do that I need to run http://www.paul.sladen.org/toys/samsung-yh-925/yh-925-db-0.1.py script, the idea behind the script is described at http://www.paul.sladen.org/toys/samsung-yh-925/ I'm getting errors and hoping someone could give me some hints, for I have no python background.
0
1659
by: cs02lk1 | last post by:
Hi, I need to stream audio and/or video to a PDA device. There is a trick here which is: The PDA must receive the stream from a multicast address. For this I have implemented a Bridge application which joins the multicast group on behalf of the PDA and receives the Multicast RTP packets (which are sent from JMStudio) and Unicasts them to the PDA.(HP iPAQ) I had no problem implementing this. The streaming is done using JMStudio player which...
0
3352
by: james.duckworthy | last post by:
Python programmers may find the application to decoding an encrypted map image format known as Memory Map to produce a standard PNG image file interesting. Someone obviously very well versed in Python and in the intricacies of image files has written a routine to decode originally the UK Landranger maps, so that they can be used with other mapping and GPS programs apart from
15
2307
by: glacier | last post by:
I use chinese charactors as an example here. "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'" My first question is : what strategy does 'decode' use to tell the way to seperate the words. I mean since s1 is an multi-bytes-char string, how did it determine to seperate the string every 2bytes or 1byte? My second question is: is there any one who has tested very long mbcs
3
3788
by: d-fan | last post by:
void decodebio( unsigned char *encbuf, unsigned char * decbuf, int destbuf ) { /* Read Base64 encoded data from standard input and write the decoded data to standard output: */ BIO *b64, *bio ; long i ; char buffer ; memset(buffer, 0, 512) ;
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.