Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. loading image from database to website

loading image from database to website

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abolfazl Sheikhloo
    wrote on last edited by
    #1

    Image img1 = getImage("select picture from database where id=1"); Image img2 = getImage("select picture from database where id=2"); pnl1.Controls.add(img1);//for example. not working; pnl1.Controls.add(img2); please help me for putting this images in part of page;

    We Can Do Anything, If We Want It

    G 1 Reply Last reply
    0
    • A Abolfazl Sheikhloo

      Image img1 = getImage("select picture from database where id=1"); Image img2 = getImage("select picture from database where id=2"); pnl1.Controls.add(img1);//for example. not working; pnl1.Controls.add(img2); please help me for putting this images in part of page;

      We Can Do Anything, If We Want It

      G Offline
      G Offline
      gauthee
      wrote on last edited by
      #2

      YOu got to convert the image right! when you say select image it would not simply return imageinstead it returns binary data

      Gautham

      A 1 Reply Last reply
      0
      • G gauthee

        YOu got to convert the image right! when you say select image it would not simply return imageinstead it returns binary data

        Gautham

        A Offline
        A Offline
        Abolfazl Sheikhloo
        wrote on last edited by
        #3

        i know that is return binary data. and i know how to convert binary data to image. but i dont know how to put converted image to page on the fly without saving images in any folder?

        We Can Do Anything, If We Want It

        T 1 Reply Last reply
        0
        • A Abolfazl Sheikhloo

          i know that is return binary data. and i know how to convert binary data to image. but i dont know how to put converted image to page on the fly without saving images in any folder?

          We Can Do Anything, If We Want It

          T Offline
          T Offline
          thomasa
          wrote on last edited by
          #4

          You can use a MemoryStream: private static int chunkSize = 10000; private void writeData(object yourImageBinaryData){ MemoryStream ms = null; int dataLeft = 0; try { dataLeft = ((byte[])yourImageBinaryData).Length; ms = new MemoryStream((byte[])yourImageBinaryData); while(dataLeft > 0 && Response.IsClientConnected) { byte[] currentChunk = new byte[chunkSize]; int currentChunkSize = ms.Read(currentChunk, 0, chunkSize); Response.BinaryWrite(currentChunk); dataLeft -= currentChunkSize; currentChunk = null; } } catch(Exception e){ throw new Exception("writeData failed. " + e.Message); }finally{ if (ms != null)ms.Close(); ms = null; } } Hope it helps Thomas

          A 1 Reply Last reply
          0
          • T thomasa

            You can use a MemoryStream: private static int chunkSize = 10000; private void writeData(object yourImageBinaryData){ MemoryStream ms = null; int dataLeft = 0; try { dataLeft = ((byte[])yourImageBinaryData).Length; ms = new MemoryStream((byte[])yourImageBinaryData); while(dataLeft > 0 && Response.IsClientConnected) { byte[] currentChunk = new byte[chunkSize]; int currentChunkSize = ms.Read(currentChunk, 0, chunkSize); Response.BinaryWrite(currentChunk); dataLeft -= currentChunkSize; currentChunk = null; } } catch(Exception e){ throw new Exception("writeData failed. " + e.Message); }finally{ if (ms != null)ms.Close(); ms = null; } } Hope it helps Thomas

            A Offline
            A Offline
            Abolfazl Sheikhloo
            wrote on last edited by
            #5

            thank's for you. but i want put all image for one user in the page and Response.BinaryWrite or Response.OutputStream dose not Suport this

            We Can Do Anything, If We Want It

            T 1 Reply Last reply
            0
            • A Abolfazl Sheikhloo

              thank's for you. but i want put all image for one user in the page and Response.BinaryWrite or Response.OutputStream dose not Suport this

              We Can Do Anything, If We Want It

              T Offline
              T Offline
              thomasa
              wrote on last edited by
              #6

              You could putt each image in DataGrid, repeater, listcontrol. You could also create your own class that contains the image from the database. Something like this: First create an empty .aspx page <%@ Page language="c#" Codebehind="MyImageClass.aspx.cs" AutoEventWireup="false" Inherits="My.Publishing.MyImageClass" %> The code behinde: using System; using System.IO; using System.Drawing.Imaging; using Millum.Procurement.Imaging; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Web; using System.Web.Caching; using System.Collections; namespace My.Publishing{ public class MyImageClass : System.Web.UI.Page{ private static int chunkSize = 10000; override protected void OnInit(EventArgs e){ InitializeComponent(); base.OnInit(e); } private void InitializeComponent(){ uploadData(); } private void uploadData(){ string strID = null; strID = (Request.QueryString["attID"] != null)?Request.QueryString["attID"]:(Request.Form["attID"] != null)?Request.Form["attID"]:null; byte[] byteImg1 = getImage("select picture from database where id=" + strID ); writeData(byteImg1); } private void writeData(byte[] yourImageBinaryData){ MemoryStream ms = null; int dataLeft = 0; try { dataLeft = yourImageBinaryData.Length; ms = new MemoryStream(yourImageBinaryData); while(dataLeft > 0 && Response.IsClientConnected) { byte[] currentChunk = new byte[chunkSize]; int currentChunkSize = ms.Read(currentChunk, 0, chunkSize); Response.BinaryWrite(currentChunk); dataLeft -= currentChunkSize; currentChunk = null; } } catch(Exception e){ throw new Exception("writeData failed. " + e.Message); }finally{ if (ms != null)ms.Close(); ms = null; } } Now the page where the images is viewed, this is an example with a DataGrid, where the image is displaide in a imagebutton: asp:datagrid id="dgImages" runat="server" CssClass="grid" AutoGenerateColumns="False" GridLines="None" ShowFooter="True"> SelectedItemStyle Font-Bold="True" CssClass="grid_itemSelected"> EditItemStyle CssClass="grid_itemEdit"> AlternatingItemStyle CssClass="grid_itemAlternating"> ItemStyle CssClass="grid_item"> HeaderStyle Font-Bold="True" CssClass="grid_header"> FooterStyle CssClass="grid_footer"> Columns> asp:TemplateColumn ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top">

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups