コード例 #1
0
        public System.IO.MemoryStream ShowImage(string costcode, int offset, ref string filepath)
        {
            DDCOST_CENT_DWG ddDrawing = null;

            byte[] photosource = null;
            System.IO.MemoryStream strm;
            try
            {
                ddDrawing = (from c in DB.DDCOST_CENT_DWG
                             where c.COST_CENT_CODE == costcode
                             select c).SingleOrDefault <DDCOST_CENT_DWG>();
                if (ddDrawing.IsNotNullOrEmpty())
                {
                    if (ddDrawing.PHOTO != null)
                    {
                        photosource = ddDrawing.PHOTO.ToArray();
                        strm        = new System.IO.MemoryStream();
                        strm.Write(photosource, offset, photosource.Length - offset);
                        filepath = ddDrawing.FILE_PATH;
                        return(strm);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch (System.Data.Linq.ChangeConflictException ex)
            {
                DB.ChangeConflicts.ResolveAll(System.Data.Linq.RefreshMode.KeepChanges);
                ex.LogException();
                return(null);
            }
            catch (Exception ex)
            {
                throw ex.LogException();
            }
        }
コード例 #2
0
        ////[DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
        ////private extern static System.UInt32 FindMimeFromData(
        ////    System.UInt32 pBC,
        ////    [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
        ////    [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
        ////    System.UInt32 cbSize,
        ////    [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
        ////    System.UInt32 dwMimeFlags,
        ////    out System.UInt32 ppwzMimeOut,
        ////    System.UInt32 dwReserverd
        ////);


        ////public string getMimeFromFile(byte[] byteArray)
        ////{

        ////    byte[] buffer = new byte[256];
        ////    using (System.IO.MemoryStream fs = new System.IO.MemoryStream(byteArray))
        ////    {
        ////        if (fs.Length >= 256)
        ////            fs.Read(buffer, 0, 256);
        ////        else
        ////            fs.Read(buffer, 0, (int)fs.Length);
        ////    }
        ////    try
        ////    {
        ////        System.UInt32 mimetype;
        ////        FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
        ////        System.IntPtr mimeTypePtr = new IntPtr(mimetype);
        ////        string mime = Marshal.PtrToStringUni(mimeTypePtr);
        ////        Marshal.FreeCoTaskMem(mimeTypePtr);
        ////        return mime;
        ////    }
        ////    catch (Exception e)
        ////    {
        ////        return e.Message;
        ////    }
        ////}


        private bool SaveDrawing(CostCenterMaster costcentermaster)
        {
            ///SaveDrawingSQL(costcentermaster);
            //return true;
            DDCOST_CENT_DWG ddDrawing = null;
            bool            submit    = false;
            bool            insert    = false;
            bool            update    = false;

            try
            {
                ddDrawing = (from c in DB.DDCOST_CENT_DWG
                             where c.COST_CENT_CODE == costcentermaster.CostCentCode
                             select c).SingleOrDefault <DDCOST_CENT_DWG>();
                if (ddDrawing.IsNotNullOrEmpty())
                {
                    ddDrawing.UPDATED_BY   = userInformation.UserName;
                    ddDrawing.UPDATED_DATE = serverDateTime;
                    ddDrawing.DELETE_FLAG  = false;
                    if (costcentermaster.Photo != null)
                    {
                        //ddDrawing.PHOTO = System.IO.File.ReadAllBytes(costcentermaster.ImageByte);
                        ddDrawing.PHOTO     = costcentermaster.ImageByte;
                        ddDrawing.FILE_PATH = costcentermaster.FilePath;
                    }
                    update = true;
                }
                else
                {
                    ddDrawing                = new DDCOST_CENT_DWG();
                    ddDrawing.ENTERED_BY     = userInformation.UserName;
                    ddDrawing.ENTERED_DATE   = serverDate;
                    ddDrawing.DELETE_FLAG    = false;
                    ddDrawing.COST_CENT_CODE = costcentermaster.CostCentCode;
                    ddDrawing.PHOTO          = costcentermaster.ImageByte;
                    ddDrawing.FILE_PATH      = costcentermaster.FilePath;
                    insert = true;
                    DB.DDCOST_CENT_DWG.InsertOnSubmit(ddDrawing);
                }
                submit = true;
                DB.SubmitChanges();
                return(true);
            }
            catch (System.Data.Linq.ChangeConflictException ex)
            {
                DB.ChangeConflicts.ResolveAll(System.Data.Linq.RefreshMode.KeepChanges);
                ex.LogException();
                return(false);
            }
            catch (Exception ex)
            {
                if (submit == true)
                {
                    if (insert == true)
                    {
                        DB.DDCOST_CENT_DWG.DeleteOnSubmit(ddDrawing);
                    }
                    if (update == true)
                    {
                        DB.DDCOST_CENT_DWG.Context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, ddDrawing);
                    }
                }
                throw ex.LogException();
            }
        }