/// <summary> /// Создать запись об изображении /// </summary> /// <param name="imagePath">Путь к изопражению</param> /// <param name="targetDir">Путь назначение</param> /// <returns>Ключ рисунка</returns> private Model.Image CreateImage(string imagePath, string targetDir, IConfiguration cnf, ILogger lg, IFIleManager fm) { var imgInfo = new FileInfo(imagePath); var imgDirPath = imgInfo.Directory.FullName; string newImgPath = ""; byte[] imageData = null; try { newImgPath = fm.CopyImg(imagePath, targetDir); } catch (IOException copyError) { lg.Add(copyError.Message); } Bitmap bmp = fm.GetThumb(imagePath); string thumbPath = fm.SaveThumb(targetDir, cnf.GetThumbDirName(), bmp, imgInfo.Name); imageData = fm.GetImageData(bmp); var im = new Model.Image() { HashCode = imgInfo.GetHashCode() , ImagePath = newImgPath , ImageTitle = imgInfo.Name , ThumbnailPath = thumbPath , Thumbnail = imageData }; return(im); }
public void FillImageInfo(List<DestinationItem> items) { if (items.Count() > 0) { string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectory); var listImage = new List<ImageDto>(); // Папка с описанием диска var baseDrivePath = string.Format(@"drive{0}", _imgInfoParams.DriveId); var drivePathTmp = Path.Combine(tempDirectory, baseDrivePath); foreach (var item in items) { var fi = new FileInfo(item.Title); string NewImgPath = ""; if (!Directory.Exists(drivePathTmp)) { Directory.CreateDirectory(drivePathTmp); } NewImgPath = Path.Combine(drivePathTmp, item.UniqGuid.ToString() + fi.Extension); if (!File.Exists(NewImgPath)) { File.Copy(item.Title, NewImgPath, true); } var NewThumbDirPath = Path.Combine(drivePathTmp, _cnf.GetThumbDirName()); var NewThumbPath = Path.Combine(NewThumbDirPath, item.UniqGuid.ToString() + fi.Extension); // Создание эскиза byte[] imageData = new byte[0]; if (_imgInfoParams.SaveThumbnails) { Bitmap bmp = _fm.GetThumb(item.Title); if (!Directory.Exists(NewThumbDirPath)) { Directory.CreateDirectory(NewThumbDirPath); } bmp.Save(NewThumbPath); if (_imgInfoParams.SaveThumbnailsToDb) { imageData = _fm.GetImageData(bmp); } } var imageInfo = new FileInfo(item.Title); listImage.Add(new ImageDto() { HashCode = imageInfo.GetHashCode(), UniqGuid = item.UniqGuid, ImagePath = Path.Combine(baseDrivePath, item.UniqGuid.ToString() + fi.Extension), ImageTitle = imageInfo.Name, Thumbnail = imageData, ThumbnailPath = Path.Combine(baseDrivePath, _cnf.GetThumbDirName(), item.UniqGuid.ToString() + fi.Extension) }); } // Сохранить в БД _dm.BulkCopyImage(listImage); // Скопировать в Темповую дирректорию Copy(drivePathTmp, Path.Combine(_cnf.GetTargetImagePath(), baseDrivePath)); // Удалить темповую дирректорию _fm.DeleteDirectory(tempDirectory); } }