I created an ASP.NET/C# application to upload an image to MySQL database. The process executes without any error but for whatever image I upload I am getting output of 100x100 white image. The procedure I followed is.
1) Created a database with field picture
and type Binary(255)
.
2) Uploaded an image as cmd.Parameters.Add("@picture", OdbcType.Binary, 255).Value = FileUpload1.FileBytes;
3) Doing above a new record is being inserted and a value something of below kind is generated.
89504e470d0a1a0a0000000d49484452000002600000010008020000009b155d400000100049444154789cd4dc05745b57a2377a15c26088d99651b2248b999959b2c0966cc9cccccc8e1ddb01439899d3a4499a869999e33037d0340d34d4b4d5dbaee7e6f6b5337367eefad67bf3adf55f676dd98e221f6b9ddffeef738e20db5cbf826c77fd3638d8eafa6587ebd79daedfc0f6afd9eefae5ab372fd6bf7db9e5e7b7075dae4daf5e1c76b98ebb5cfb7ef935a5b31b028b32ea53f6ec3a77efe60fb919156e34222b297ee3aedd2e97ebe6dd870b96acd8b0efc0891bb76ae7ce8ba9a8dc70f1f2c917afaeb95ce75c1f276cd988b0180329c4c4aaf2d2
//--------------- Uploading module completed -----------------//
1) Created a ASPX page with
<asp:Image ID="img" runat="server" ImageUrl="~/MyImage.ashx" />
2) Left ASPX.CS file without any code
3) Added a ASHX file with
<%@ WebHandler Language="C#" Class="MyImage" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing.Imaging;
public class MyImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/png";
var data = "89504e470d0a1a0a0000000d49484452000002600000010008020000009b155d400000100049444154789cd4dc05745b57a2377a15c26088d99651b2248b999959b2c0966cc9cccccc8e1ddb01439899d3a4499a869999e33037d0340d34d4b4d5dbaee7e6f6b5337367eefad67bf3adf55f676dd98e221f6b9ddffeef738e20db5cbf826c77fd3638d8eafa6587ebd79daedfc0f6afd9eefae5ab372fd6bf7db9e5e7b7075dae4daf5e1c76b98ebb5cfb7ef935a5b31b028b32ea53f6ec3a77efe60fb919156e34222b297ee3aedd2e97ebe6dd870b96acd8b0efc0891bb76ae7ce8ba9a8dc70f1f2c917afaeb95ce75c1f276cd988b0180329c4c4aaf2d2";
var buffer = StringToByteArray(data);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
private byte[] StringToByteArray(string hex)
{
return Enumerable
.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public bool IsReusable
{
get { return false; }
}
}
By executing this code a white 100x100 image is being displayed in output for any colorful input. I double checked ContentType
and uploaded various images of various sizes and formats but whatever I do I am getting an output of same White image. What's wrong in my code? any corrections?