public static int SaveUserImage(Ctx ctx, byte[] buffer, string imageName, bool isSaveInDB, int userID) { MUser user = new MUser(ctx, userID, null); int imageID = Util.GetValueOfInt(user.GetAD_Image_ID()); MImage mimg = new MImage(ctx, imageID, null); mimg.ByteArray = buffer; mimg.ImageFormat = imageName.Substring(imageName.LastIndexOf('.')); mimg.SetName(imageName); if (isSaveInDB) { mimg.SetBinaryData(buffer); mimg.SetImageURL(string.Empty); } else { //mimg.SetImageURL(HostingEnvironment.MapPath(@"~/Images/100by100"));//Image Saved in File System so instead of byteArray image Url will be set mimg.SetImageURL("Images/Thumb100x100");//Image Saved in File System so instead of byteArray image Url will be set mimg.SetBinaryData(new byte[0]); } if (!mimg.Save()) { return(0); } user.SetAD_Image_ID(mimg.GetAD_Image_ID()); if (!user.Save()) { return(0); } return(mimg.GetAD_Image_ID()); }
/// <summary> /// Save Image into data base/folder /// </summary> /// <param name="ctx"></param> /// <param name="buffer">image Byte array</param> /// <param name="imageID">image ID for the file</param> /// <param name="imageName">name of the i,age</param> /// <param name="isSaveInDB">is want to save into the data base</param> /// <returns></returns> public static int SaveImage(Ctx ctx, byte[] buffer, int imageID, string imageName, bool isSaveInDB) { MImage mimg = new MImage(ctx, imageID, null); mimg.ByteArray = buffer; mimg.ImageFormat = imageName.Substring(imageName.LastIndexOf('.')); mimg.SetName(imageName); if (isSaveInDB) { mimg.SetBinaryData(buffer); //mimg.SetImageURL(string.Empty); } else { // if user uncheck the save in db checkbox mimg.SetBinaryData(null); } mimg.SetImageURL("Images"); //else //{ // mimg.SetImageURL("Images");//Image Saved in File System so instead of byteArray image Url will be set // mimg.SetBinaryData(new byte[0]); //} if (!mimg.Save()) { return(0); } return(mimg.Get_ID()); }
//public static int SaveUserImage(Ctx ctx, byte[] buffer, string imageName, bool isSaveInDB, int productID) public static string SaveUserImage(Ctx ctx, byte[] buffer, string imageName, bool isSaveInDB, int productID) { string imageDataURL = null; MProduct user = new MProduct(ctx, productID, null); int imageID = Util.GetValueOfInt(user.GetAD_Image_ID()); //DirectoryInfo dir = new DirectoryInfo(HostingEnvironment.MapPath("~/TempFiles/ProductImages/Thumb100x100")); MImage mimg = new MImage(ctx, imageID, null); mimg.ByteArray = buffer; mimg.ImageFormat = imageName.Substring(imageName.LastIndexOf('.')); mimg.SetName(imageName); if (isSaveInDB) { mimg.SetBinaryData(buffer); //mimg.SetImageURL(string.Empty); } mimg.SetImageURL("Images/Thumb46x46");//Image Saved in File System so instead of byteArray image Url will be set if (!mimg.Save()) { return(""); } user.SetAD_Image_ID(mimg.GetAD_Image_ID()); if (!user.Save()) { return(""); } else { string filePath = Path.Combine(HostingEnvironment.MapPath("~/Images/Thumb46x46"), mimg.GetAD_Image_ID() + mimg.ImageFormat); if (File.Exists(filePath)) { byte[] imageByteData = System.IO.File.ReadAllBytes(filePath); string imageBase64Data = Convert.ToBase64String(imageByteData); imageDataURL = string.Format("data:image/png;base64,{0}", imageBase64Data); } } return(imageDataURL); //return mimg.GetAD_Image_ID(); }