Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) CreateBmpFromChart (Read 8723 times)
krebsd
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 13
Joined: Dec 9th, 2005
CreateBmpFromChart
Dec 16th, 2005 at 8:23am
Print Post  
For my Application I use the ImageGen of the WebApp-VB-Sample.
My problem is, that the ImageGen creates a bmp-File. Is it possible to create jpg's or gif's?
Now my bmp's have up to 3 MB. that's to much.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateBmpFromChart
Reply #1 - Dec 16th, 2005 at 10:34am
Print Post  
The Save method of the bitmap class has a parameter that lets you specify the file format, e.g.

generatedBmp.Save(fileName, ImageFormat.Jpeg)
  
Back to top
 
IP Logged
 
krebsd
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 13
Joined: Dec 9th, 2005
Re: CreateBmpFromChart
Reply #2 - Dec 16th, 2005 at 10:44am
Print Post  
well, but in the browser is the bitmap shown.
I clicked with right-mouse --> save as. Then I could store it as bitmap only.
Is the image created clientside or is it sent from the server to the client?
If it would be sent, that would be much traffic.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateBmpFromChart
Reply #3 - Dec 16th, 2005 at 11:17am
Print Post  
This code should create a jpeg image:

[code]
private void Page_Load(object sender, System.EventArgs e)
{
  Response.ContentType = "image/jpeg";
  Response.Cache.SetCacheability(HttpCacheability.NoCache);

  // generate jpeg image ...
  FlowChart fc = (FlowChart)this.Session["fcx"];
  if (fc !=  null)
  {
  Bitmap bmp = fc.CreateBmpFromChart();

  // ... and send it to the browser
  EncoderParameters eps = new   EncoderParameters(1);
  eps.Param[0] = new EncoderParameter(   Encoder.Quality, (long)98 );
  ImageCodecInfo codec = GetEncoderInfo(Response.ContentType);
  bmp.Save(Response.OutputStream, codec, eps);
  bmp.Dispose();
}

  Response.OutputStream.Close();
}
[/code]

It sets the http response type to "image/jpeg" and uses an encoder for the "image/jpeg" MIME type to create the image data. Now that might be a problem in the browser if it determines the image type by the linked image file name extension and not by the MIME type.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateBmpFromChart
Reply #4 - Dec 16th, 2005 at 11:21am
Print Post  
You could test that by using a lower image quality parameter. E.g. set it to 50 and reload the application. If the image seems blurred, then it's probably a jpeg but the browser saves it as a bmp.
  
Back to top
 
IP Logged
 
Erison Liang
Guest


Fail to save as PNG format Re: CreateBmpFromChart
Reply #5 - Jan 11th, 2006 at 12:56pm
Print Post  
Hi,

It will throw an exception if try to save as PNG format. The following is my code snipet:

private void Page_Load(object sender, System.EventArgs e)
{
  Response.ContentType = "image/png";
  Response.Cache.SetCacheability(HttpCacheability.NoCache);

  // generate jpeg image ...
  FlowChart fc = (FlowChart)this.Session["fcx"];
  if (fc !=  null)
  {
    Bitmap bmp = fc.CreateBmpFromChart();
    bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
    bmp.Dispose();
  }
  Response.OutputStream.Close();
}
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateBmpFromChart
Reply #6 - Jan 11th, 2006 at 1:10pm
Print Post  
Hi,

What is the exception type? can you trace that with the original code from the Webapp sample to see whether there is a PNG encoder found on your system ?

  
Back to top
 
IP Logged
 
Erison Liang
Guest


Re: CreateBmpFromChart
Reply #7 - Jan 11th, 2006 at 1:34pm
Print Post  
I think the exception type is "System.Runtime.InteropServices.ExternalException". The StackTrace is shown below:
    at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
    at System.Drawing.Image.Save(Stream stream, ImageFormat format)
    at WebApp.ImageGen.Page_Load(Object sender, EventArgs e)
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateBmpFromChart
Reply #8 - Jan 11th, 2006 at 2:14pm
Print Post  
It seems the PNG encoder requires a seekable stream as explained here:

http://aspalliance.com/319

Try saving the PNG to a MemoryStream and then write the memStream.ToArray() to the Response.OutputStream.
  
Back to top
 
IP Logged
 
Erison Liang
Guest


Re: CreateBmpFromChart
Reply #9 - Jan 12th, 2006 at 10:40pm
Print Post  
It works now. Thanks to you.

- Erison
  
Back to top
 
IP Logged
 
krebsd
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 13
Joined: Dec 9th, 2005
Re: CreateBmpFromChart
Reply #10 - Mar 31st, 2006 at 5:49am
Print Post  
Stoyan,

you wrote: "You could test that by using a lower image quality parameter. E.g. set it to 50 and reload the application. If the image seems blurred, then it's probably a jpeg but the browser saves it as a bmp. "

Is there any possibility that the browser doesn't save the image as .bmp?
My problem is, that a flowchart with size 1800x1200 will be a 6.18 MB Bitmap. In a normal-Quality jpeg it would be 100 KB.

regards, Daniel.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: CreateBmpFromChart
Reply #11 - Mar 31st, 2006 at 6:34am
Print Post  
Daniel,

FireFox saves the generated image as a jpeg, and both IE6 and IE7 save it as bmp. So it seems that's an IE problem. You might try mapping an http://.../diag.jpg URL to the ASP page that generates the image. Probably that will be a better clue to IE that the image is a jpeg than the mime type. Or just create temporary images in a server folder and make the IMG tags link to these images.

Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint