WebImage(string fileName, string cachePath) { string ext = Path.GetExtension(fileName); FileName = fileName; MimeType = MimeTypes.FromExtension(ext); this.LogDebug("Loading Bitmap32 from file {0}", fileName); Data = File.ReadAllBytes(fileName); //read thumb if present var ThumbFileName = FileSystem.Combine(cachePath, Path.GetFileNameWithoutExtension(fileName) + "-thumb" + ext); if (File.Exists(ThumbFileName)) { //but check if the image was changed first. if (FileSystem.GetLastWriteTimeUtc(ThumbFileName) > FileSystem.GetLastWriteTimeUtc(fileName)) { ThumbData = File.ReadAllBytes(ThumbFileName); return; } } //need to recreate thumb using (var bmp = Bitmap32.Create(Data)) { switch (MimeType) { case "image/jpeg": ThumbData = RenderThumb(bmp, ThumbFileName, ImageType.Jpeg); break; case "image/png": ThumbData = RenderThumb(bmp, ThumbFileName, ImageType.Png); break; default: throw new NotImplementedException(); } } }
/// <summary>Tries to load the image data.</summary> /// <param name="data">The data.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">data</exception> public byte[] TryLoadImageData(byte[] data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } try { using (var img = Bitmap32.Create(data)) { return(data); } } catch { return(null); } }
/// <summary> /// Updates the texture and scale of the underlying Object3D. /// </summary> /// <param name="maxWidth">max width in pixels.</param> /// <param name="maxHeight">max height in pixels.</param> public void Update(int maxWidth = 0, int maxHeight = 0) { if (Sprite == null) { throw new ObjectDisposedException("RenderText"); } Trace.TraceInformation("Update text font:{0} size:{1} fg:{2} bg:{3} text:{4}", FontName, FontSize, ForeColor, BackColor, Text); var bitmap = Bitmap32.Create(FontName, FontSize, ForeColor, BackColor, Text); if ((maxWidth > 0) && (maxHeight > 0)) { if ((bitmap.Width > maxWidth) || (bitmap.Height > maxHeight)) { var b = new Bitmap32(Math.Min(bitmap.Width, maxWidth), Math.Min(bitmap.Height, maxHeight)); b.Draw(bitmap, 0, 0); bitmap.Dispose(); bitmap = b; } } Sprite.LoadTexture(bitmap); Sprite.Scale = Sprite.ScaleFromSize(bitmap.Width, bitmap.Height); }
bool SaveImage(byte[] data, MDBFolder mdbFolder, string name, ref MDBImage image, object obj) { string fullPath; var file = new MDBFile() { FolderID = mdbFolder.ID, Name = name, }; ImageType imgType; switch (image.Type) { case MDBImageType.ArtistMusicBanner: case MDBImageType.ArtistMusicLogo: case MDBImageType.ArtistMusicLogoHD: case MDBImageType.AlbumCDArt: fullPath = mdbFolder.GetFullPath(this, name + ".png"); file.Extension = ".png"; file.FileType = MDBFileType.png; imgType = ImageType.Png; image.MimeType = "image/png"; break; default: fullPath = mdbFolder.GetFullPath(this, name + ".jpg"); file.Extension = ".jpg"; imgType = ImageType.Jpeg; file.FileType = MDBFileType.jpeg; image.MimeType = "image/jpg"; break; } int width, height; using (var img = Bitmap32.Create(data)) { width = img.Width; height = img.Height; //save if not present at disk if (!File.Exists(fullPath)) { using (var ms = new MemoryStream()) { img.Save(ms, imgType, 99); data = ms.ToArray(); } } } bool writeFile = true; //find old dataset (check for replace) { if (TryGetFile(fullPath, false, out MDBFile mdbFile)) { file.ID = mdbFile.ID; if (mdbFile.GetFullPath(this) == fullPath) { writeFile = false; } else { string oldPath = mdbFile.GetFullPath(this); File.Delete(oldPath); } } } //save image data if (writeFile) { foreach (string oldFile in Directory.GetFiles(Path.GetDirectoryName(fullPath), Path.GetFileNameWithoutExtension(fullPath) + ".*")) { File.Delete(oldFile); if (TryGetFile(oldFile, false, out MDBFile mdbFile)) { Files.Delete(mdbFile.ID); } this.LogInfo("Deleted old file {0}", oldFile); } File.WriteAllBytes(fullPath, data); this.LogInfo("Saved new image {0}", fullPath); } //get fileinfo var fileInfo = new FileInfo(fullPath); //create file dataset file.DateTime = fileInfo.LastWriteTimeUtc; file.Size = fileInfo.Length; if (file.ID > 0) { Files.Replace(file); } else { file.ID = Files.Insert(file); } //update image dataset image.Width = width; image.Height = height; image.FileID = file.ID; if (Images.Exist(file.ID)) { Images.Replace(image); this.LogNotice("<cyan>Update {0} image<default> dataset for <yellow>{1} <default>{2}", image, obj, mdbFolder); return(false); } else { Images.Insert(image); this.LogNotice("<green>New {0} image<default> dataset for <yellow>{1} <default>{2}", image, obj, mdbFolder); return(true); } }