locked
how Render an image ??? RRS feed

  • Question

  •  

    how can we render an image in our custom web parts?

    I have a piece of code as bellow in my web part :

     

    NPlot.Bitmap.PlotSurface2D plotSurface = new NPlot.Bitmap.PlotSurface2D(328, 240);

    int seed = 0;

    Random r = new Random(seed);

    int[] data = new int[Convert.ToInt32(15)];

    for (int i = 0; i < data.Length; ++i)

    data = r.Next(30);

    LinePlot lp = new LinePlot();

    lp.DataSource = data;

    plotSurface.Add(lp);

    plotSurface.BackColor = Color.White;

    plotSurface.Title = "title";

    plotSurface.Refresh();

    Context.Response.ContentType = "image/png";

    MemoryStream m = plotSurface.ToStream(System.Drawing.Imaging.ImageFormat.Png);

    m.WriteTo(Context.Response.OutputStream);

     

     

    but the result is something like that:

     

    �PNG  IHDRH�b���sRGB���gAMA�� �a cHRMz&�����u0�`:�p��Q<bIDATx^휁�ܸ D/����M6�iei� ����l�@�5�-͌�_����_�� �? |�_� �'���@�HN �$G��� `#����!����ͨ���i��j@`#�Ff3* `#@@�x�؈��ٌ ��6^��6"@@ndv�Q��)�����Z��ж>r}_1�5�C���x�!�%m

    Saturday, February 2, 2008 5:00 AM

Answers

  • I dont think you will be able to use this code to create an image in the web part. The response for the page is set too late and you cannot have a "sub response" type for a web part. The Web Part dumps data to the Web Part Page output stream, You are dumping bindary data into a HTML stream where the browser is expecting html.

     

    Possible soluition - instead of creating the code and attempting to dump the binary to output ( where the browser will interp it as html vrs binary) you create and save the image then in the web part output an image control and include the created image url as the source.

     

    Might also consider using the Web Part to dump out and image with a URL and the URL end point is handled by an HTTP Handler which uses your code above to generate the image and send it down as a PNG type.

     

    Here is an interesting link that might explain the concept of a HTTP handler better.

     

    http://msdn.microsoft.com/msdnmag/issues/04/04/CuttingEdge/

     

     

     

    Monday, February 4, 2008 1:02 AM