Wednesday, November 2, 2011

How to Convert Image as Binary and Binary to Image

string StrExtension = Path.GetExtension(FUHeaderLogo.PostedFile.FileName.ToString());
byte[] ImgBinary = System.IO.File.ReadAllBytes(FUHeaderLogo.PostedFile.FileName.ToString());

Method 1

void GetPrescriptionImages(int AppId, int CenterId)
{

DataTable DtImgs = ObjAppointmentBL.GetPrescriptionImages(AppId, CenterId);
if (DtImgs.Rows.Count > 0)
{
Session["PrescriptionUrl"] = DtImgs.Rows[0]["PrescriptionUrl"].ToString();
foreach (DataRow _Row in DtImgs.Rows)
{
string[] _Url = _Row[1].ToString().Split('\\');
string DirectoryPath = System.Web.Hosting.HostingEnvironment.MapPath("~/ScanImagePrescriptions/") + _Url[2].ToString() + "\\";
if (Directory.Exists(DirectoryPath) == false)
{
Directory.CreateDirectory(DirectoryPath);
}
if (!File.Exists(DirectoryPath + _Url[3].ToString()))
{
byte[] _Img = (byte[])_Row[2];
MemoryStream ms = new MemoryStream(_Img);
FileStream fs = new FileStream
 (DirectoryPath + _Url[3].ToString(), FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();
}
}
}
else
Session["PrescriptionUrl"] = "";
}

Method 2

void CreateBinayImages(int CenterId, string filename, DataTable DtBinaryImgs)
{
try
{
if (DtBinaryImgs.Rows.Count > 0)
{
string DirectoryPath = filename.Replace("DownLoadReports.pdf", "");
byte[] _Img = (byte[])DtBinaryImgs.Rows[0]["ImgHeaderBinayByte"];
MemoryStream ms = new MemoryStream(_Img);
FileStream fs = new FileStream
 (DirectoryPath + Convert.ToString(CenterId) + DtBinaryImgs.Rows[0]["ImgExtension"].ToString(), FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();
}
}
catch (Exception Ex)
{
}
}

Method 3

public void GetLoadedImages()
{
try
{
DataSet DsImages = ObjUsersBL.SelectDataSet(Convert.ToString(Session["_ConString"]), "Select ImgHeaderBinayByte,ImgExtension from tblCentreSelection where Centreid='" + Session["_SelCen"] + "'");
if (DsImages.Tables[0].Rows.Count > 0)
{
string DirectoryPath = System.Web.Hosting.HostingEnvironment.MapPath("~/HeaderLogos/");
byte[] _Img = (byte[])DsImages.Tables[0].Rows[0]["ImgHeaderBinayByte"];
MemoryStream ms = new MemoryStream(_Img);
FileStream fs = new FileStream
 (DirectoryPath + "\\" + Session["_SelCen"].ToString() + DsImages.Tables[0].Rows[0]["ImgExtension"].ToString(), FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();
if (File.Exists(DirectoryPath + Session["_SelCen"].ToString() + DsImages.Tables[0].Rows[0]["ImgExtension"].ToString()))
{
ImgView.Visible = true;
ImgView.ImageUrl = "~/HeaderLogos/" + Session["_SelCen"].ToString() + DsImages.Tables[0].Rows[0]["ImgExtension"].ToString();
}
}
}
catch (Exception Ex)
{
log.Info("GetLoadedImages() : ADMIN : ImgHeaderLogoUpdater.aspx", Ex);
}
}

No comments:

Post a Comment