Click here to Skip to main content
15,745,569 members
Articles / Programming Languages / C#
Posted 10 Jun 2010

Tagged as


3 bookmarked

WebBrowser shows File-less local Image

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
11 Jun 2010CPOL
Showing an image in a WinForm WebBrowser without using files
A System.Windows.Forms.WebBrowser can be used to browse any web page we want, by using its Navigate() method. It can also be used to display information, real or synthetic, to either its DocumentText or DocumentStream property.

One way to make WebBrowser show a document would be to create a big string starting with <html> and adhering to the HTML specifications; then assigning said string to WebBrowser.DocumentText

However browsers such as Internet Explorer, and hence also WebBrowser, have sufficient intelligence to recognize several different data types, so when the web page isn't actually providing a valid HTML document, but just a simple object, such as an image, the browser will recognize it and handle it well.

Here is some demo code, that creates an image in a MemoryStream, and shows it in a little Form+WebBrowser by using the latter's DocumentStream property.

void CreateAndShowImageInFormAndWebBrowser() {
	Form f=new Form();
	// create an image
	Bitmap bm=new Bitmap(200, 200);
	Graphics g=Graphics.FromImage(bm);
	g.FillRectangle(Brushes.Yellow, new Rectangle(20, 20, 160, 160));
	g.DrawString("Hello World!", f.Font, Brushes.Black, 40, 40);
	// store the image in a memory stream
	MemoryStream ms=new MemoryStream();
	bm.Save(ms, ImageFormat.Gif);
	// create a web browser that displays the stream
	WebBrowser wb=new WebBrowser();
	wb.Bounds=new Rectangle(20, 20, 500, 500);
	// tada
	f.Size=new Size(600, 600);

Warning: you must "rewind" the stream (like a tape recorder!); without the ms.Position=0; statement, the WebBrowser window would remain very blank.

ADDED: image test using <img src="/script/Membership/Uploads/648011/kitten-high-five.jpg" />



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Belgium Belgium
I am an engineer with a background in electronics, software and mathematics.

I develop technical software, both for embedded systems and for desktop equipment. This includes operating systems, communication software, local networks, image processing, machine control, automation, etc.

I have been using all kinds of microcontrollers and microprocessors (Intel 4004/8080/8051/80386/Pentium, Motorola 680x/680x0/ColdFire/PowerPC, Microchip PIC, Altera NIOS, and many more), lots of programming languages (all relevant assemblers, Fortran, Basic, C, Java, C#, and many more), and different operating systems (both proprietary and commercial).

For desktop applications and general development tools I have been using both UNIX systems and Mac/MacOS for many years, but I have switched to x86-based PCs with Windows, Visual Studio and the .NET Framework several years ago.

I specialize in:
- cross-platform development (making software that runs on diverse hardware/OS combinations)
- instruction set simulation
- improving software performance, i.e. making sure the software runs the job at hand in as short a time as possible on the given hardware. This entails algorithm selection, implementation design, accurate measurements, code optimisation, and sometimes implementing virtual machines, applying SIMD technology (such as MMX/SSE), and more.

Comments and Discussions

QuestionStreaming a non-native file Pin
JFish22216-May-12 13:07
JFish22216-May-12 13:07 
AnswerRe: Streaming a non-native file Pin
Luc Pattyn16-May-12 17:59
sitebuilderLuc Pattyn16-May-12 17:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.