private static byte[] GetNewBitmapBytes() { Bitmap pic = new Bitmap(BMP_WIDTH, BMP_HEIGHT); using(Graphics gr = Graphics.FromImage(pic)) { Rectangle imgBgr = new Rectangle(0, 0, BMP_WIDTH, BMP_HEIGHT); gr.FillRectangle(Brushes.White, imgBgr); } var picBytes = pic.ToByteArray(ImageFormat.Bmp); return picBytes; }
/// <summary> /// Recorta uma imagem a partir das coordenadas do retangulo informado /// </summary> /// <param name="source">Bitmap a ser recortado</param> /// <param name="section">Retangulo com as coordenadas do recorte</param> /// <returns>byte arrah da imagem recortada</returns> public static byte[] Crop(this Bitmap source, Rectangle section) { // An empty bitmap which will hold the cropped image Bitmap bmp = new Bitmap(section.Width, section.Height); Graphics g = Graphics.FromImage(bmp); // Draw the given area (section) of the source image // at location 0,0 on the empty bitmap (bmp) g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel); return bmp.ToByteArray(); }
private Response ProcessAsset() { var file = Request.Files.First(); var bitmap = new Bitmap(file.Value); var response = new Response(); byte[] bytes = bitmap.ToByteArray(); response.StatusCode = HttpStatusCode.OK; response.ContentType = "image/png"; response.Contents = stream => stream.Write(bytes, 0, bytes.Length); return response; }
private void CreateFontTexture(MiyagiSystem system, SD.FontStyle fontStyle) { using (var pfc = new PrivateFontCollection()) { pfc.AddFontFile(this.FileName); int leading = this.Leading; using (SD.Bitmap fontBitmap = CreateFontBitmap(pfc.Families[0], fontStyle, this.Size, this.Resolution, ref leading, this.codePoints, this.GlyphCoordinates)) { this.Leading = leading; GlobalGlyphCoordinates[this.TextureName] = this.GlyphCoordinates; GlobalLeadings[this.TextureName] = this.Leading; this.TextureHandle = system.Backend.CreateTexture(this.TextureName, new Size(fontBitmap.Size.Width, fontBitmap.Size.Height)); system.Backend.WriteToTexture(fontBitmap.ToByteArray(), this.TextureHandle); } } }
public ActionResult ReturnNSFW(ImageData thisImage) { if (thisImage != null && thisImage.Data.Length >= 10) { Assembly _assembly = Assembly.GetExecutingAssembly(); Stream? _imageStream = _assembly.GetManifestResourceStream("APLoffredo.Content.Images.placeholder.jpg"); if (_imageStream is null) { throw new Exception("Placeholder image stream not found"); } using Drawing.Bitmap original = new Drawing.Bitmap(_imageStream); using Drawing.Bitmap resized = new Drawing.Bitmap(original, new Drawing.Size(thisImage.Width, thisImage.Height)); return(this.File(resized.ToByteArray(), "image/jpeg")); } else { return(this.Redirect("/Content/Image/placeholder.jpg")); } }
private Response CreateCache() { var cache = new CacheModel(); var imageModel = new ImageModel(); var file = Request.Files.First(); var original = new Bitmap(file.Value); var processed = ImageProcessor.ApplyFilters(original); imageModel.Id = Guid.NewGuid(); imageModel.OriginalImageId = Guid.NewGuid().ToString(); imageModel.OriginalImageThumbnailId = Guid.NewGuid().ToString(); imageModel.ProcessedImageId = Guid.NewGuid().ToString(); imageModel.ProcessedImageThumbnailId = Guid.NewGuid().ToString(); cache.Id = Guid.NewGuid(); cache.Images = new List<Guid>(new []{ imageModel.Id }); cache.CreatorId = Guid.Empty; cache.CreatedDate = DateTime.Now; using (var ms = new MemoryStream(original.ToByteArray())) _documentStore.DatabaseCommands.PutAttachment(imageModel.OriginalImageId, null, ms, new RavenJObject()); using (var ms = new MemoryStream(processed.ToByteArray())) _documentStore.DatabaseCommands.PutAttachment(imageModel.ProcessedImageId, null, ms, new RavenJObject()); using (var ms = new MemoryStream(original.GenerateThumbnail(80, 80).ToByteArray())) _documentStore.DatabaseCommands.PutAttachment(imageModel.OriginalImageThumbnailId, null, ms, new RavenJObject()); using (var ms = new MemoryStream(processed.GenerateThumbnail(80, 80).ToByteArray())) _documentStore.DatabaseCommands.PutAttachment(imageModel.ProcessedImageThumbnailId, null, ms, new RavenJObject()); _images.Create(imageModel); _caches.Create(cache); return Response.AsJson(cache); }
public static byte[] ToByteArray(this Bitmap bitmap) { return(bitmap.ToByteArray(System.Drawing.Imaging.ImageFormat.Png)); }
/// <summary> /// Publish an image. /// </summary> /// <param name="Image">The image.</param> /// <param name="PreviewImage">Indicates whether this is a preview image.</param> /// <param name="Number">The number, when not a preview image.</param> public ComicInfoPage Publish(byte[] Image, bool PreviewImage, int Number) { // Create a bitmap from the byte array. Bitmap Bitmap = Image.ToBitmap(); // Attempt the following code. try { // Initialize the image height. int? ImageHeight = null; // Initialize the image width. int? ImageWidth = null; // Initialize the page valid status. bool IsValid = true; // Check if the bitmap is invalid. if (Bitmap == null) { // Write the message. Console.WriteLine("Shredded {0}:#{1}", Path.GetFileName(_FilePath), Number.ToString("000")); // Set the page valid status to false. IsValid = false; // Initialize a new instance of the Bitmap class. using (Bitmap BrokenBitmap = new Bitmap(128, 32)) { // Initialize a new instance of the Graphics class. using (Graphics Graphics = Graphics.FromImage(BrokenBitmap)) { // Initialize a new instance of the RectangleF class. RectangleF RectangleF = new RectangleF(0, 0, BrokenBitmap.Width, BrokenBitmap.Height); // Initialize a new instance of the Font class. Font Font = new Font("Tahoma", 10); // Initialize a new instance of the StringFormat class. StringFormat StringFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; // Fill the rectangle with a white brush. Graphics.FillRectangle(Brushes.White, RectangleF); // Write the broken page information with a black brush. Graphics.DrawString(string.Format("Broken page #{0}", Number.ToString("000")), Font, Brushes.Black, RectangleF, StringFormat); } // Set the image. Image = BrokenBitmap.ToByteArray("png"); // Set the image height. ImageHeight = BrokenBitmap.Height; // Set the image width. ImageWidth = BrokenBitmap.Width; } } else if (!PreviewImage) { // Indicating whether saving is required. bool RequiresSave = false; // Check if animation framing is not disabled. if (!_Options.DisableAnimationFraming) { // Frame to the last available frame. Bitmap Result = Bitmap.Frame(); // Check if the result is modified. if (Bitmap != Result) { // Set the bitmap. Bitmap = Result; // Set the required save state. RequiresSave = true; } } // Check if footer incision is not disabled. if (!_Options.DisableFooterIncision && string.Equals(_Provider.UniqueIdentifier, "MangaFox")) { // Crop the image to remove a textual addition. Bitmap Result = Bitmap.Crop(); // Check if the result is modified. if (Bitmap != Result) { // Set the bitmap. Bitmap = Result; // Set the required save state. RequiresSave = true; } } // Check if this is a platform compatible with image manipulation. if (PlatformID.MacOSX != Environment.OSVersion.Platform && PlatformID.Unix != Environment.OSVersion.Platform) { // Check if image processing is not disabled if (!_Options.DisableImageProcessing) { // Sharpen using a gaussian sharpen filter. Bitmap = Bitmap.Sharpen(); // Reduce noise using conservative smoothing. Bitmap = Bitmap.Noise(); // Adjust contrast in RGB colour space. Bitmap = Bitmap.Contrast(); // Linear correction in RGB colour space. Bitmap = Bitmap.Colour(); // Set the required save state. RequiresSave = true; } // Check if grayscale size comparison and save is not disabled. if (!_Options.DisableGrayscaleSizeComparisonAndSave) { // Determine whether the image is a PNG. bool IsPNG = Image.DetectImageFormat().Equals("png"); // Check if this is either a PNG image or an image that requires saving. if (IsPNG || RequiresSave) { // Convert RGB colour space to grayscale when applicable. Bitmap = Bitmap.Grayscale(); // Check if the image is a PNG but does not require to be saved. if (IsPNG && !RequiresSave) { // Create a byte array from the bitmap. byte[] GrayscaleImage = Bitmap.ToByteArray(Image.DetectImageFormat()); // Check if the grayscale image has a smaller file size. if (GrayscaleImage.Length < Image.Length) { // Set the grayscale image. Image = GrayscaleImage; } } } } } // Check if saving is required. if (RequiresSave) { // Create a byte array from the bitmapy. Image = Bitmap.ToByteArray(Image.DetectImageFormat()); } } // Save the image. if (true) { // Initialize the file name. string Key = string.Format("{0}.{1}", Number.ToString("000"), Image.DetectImageFormat()); // Increment the number of pages. _NumberOfPages++; // Begin updating the compressed file. _ZipFile.BeginUpdate(); // Attempt to delete files matching the file name and extension. _ZipFile.TryDelete(Number.ToString("000"), "bmp", "gif", "jpg", "png"); // Add the file. _ZipFile.Add(new DataSource(Image), Key); // End updating the compressed file. _ZipFile.CommitUpdate(); // Return comic page information ... return new ComicInfoPage { // ... with the image ... Image = Number, // ... with the image height ... ImageHeight = ImageHeight ?? Bitmap.Height, // ... with the image size ... ImageSize = Image.Length, // ... with the image width ... ImageWidth = ImageWidth ?? Bitmap.Width, // ... with the key ... Key = Key, // ... with the type. Type = IsValid ? (PreviewImage ? "FrontCover" : null) : "Deleted" }; } } finally { // Check if the bitmap is valid. if (Bitmap != null) { // Dispose of the bitmap. Bitmap.Dispose(); } } }
/// <summary> /// Publish an image. /// </summary> /// <param name="image">The image.</param> /// <param name="previewImage">Indicates whether this is a preview image.</param> /// <param name="number">The number, when not a preview image.</param> public ComicInfoPage Publish(byte[] image, bool previewImage, int number) { var bitmap = image.ToBitmap(); try { var imageHeight = (int?) null; var imageWidth = (int?) null; var isValid = true; if (bitmap == null) { Console.WriteLine("Shredded {0}:#{1}", Path.GetFileName(_filePath), number.ToString("000")); isValid = false; using (var brokenBitmap = new Bitmap(128, 32)) { using (var graphics = Graphics.FromImage(brokenBitmap)) { var rectangleF = new RectangleF(0, 0, brokenBitmap.Width, brokenBitmap.Height); var font = new Font("Tahoma", 10); var stringFormat = new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center}; graphics.FillRectangle(Brushes.White, rectangleF); graphics.DrawString(string.Format("Broken page #{0}", number.ToString("000")), font, Brushes.Black, rectangleF, stringFormat); } image = brokenBitmap.ToByteArray("png"); imageHeight = brokenBitmap.Height; imageWidth = brokenBitmap.Width; } } else if (!previewImage) { var requiresSave = false; if (!_options.DisableAnimationFraming) { var result = bitmap.Frame(); if (bitmap != result) { bitmap = result; requiresSave = true; } } if (!_options.DisableFooterIncision && string.Equals(_provider.Location, "http://mangafox.me/")) { var result = bitmap.Crop(); if (bitmap != result) { bitmap = result; requiresSave = true; } } if (PlatformID.MacOSX != Environment.OSVersion.Platform && PlatformID.Unix != Environment.OSVersion.Platform) { if (!_options.DisableImageProcessing) { bitmap = bitmap.Sharpen(); bitmap = bitmap.Noise(); bitmap = bitmap.Contrast(); bitmap = bitmap.Colour(); requiresSave = true; } if (!_options.DisableGrayscaleSizeComparisonAndSave) { var isPng = image.DetectImageFormat().Equals("png"); if (isPng || requiresSave) { bitmap = bitmap.Grayscale(); if (isPng && !requiresSave) { var grayscaleImage = bitmap.ToByteArray(image.DetectImageFormat()); if (grayscaleImage.Length < image.Length) { image = grayscaleImage; } } } } } if (requiresSave) { image = bitmap.ToByteArray(image.DetectImageFormat()); } } if (true) { var key = string.Format("{0}.{1}", number.ToString("000"), image.DetectImageFormat()); _numberOfPages++; _zipFile.BeginUpdate(); _zipFile.TryDelete(number.ToString("000"), "bmp", "gif", "jpg", "png"); _zipFile.Add(new DataSource(image), key); _zipFile.CommitUpdate(); return new ComicInfoPage { Image = number, ImageHeight = imageHeight ?? bitmap.Height, ImageSize = image.Length, ImageWidth = imageWidth ?? bitmap.Width, Key = key, Type = isValid ? (previewImage ? "FrontCover" : null) : "Deleted" }; } } finally { if (bitmap != null) { bitmap.Dispose(); } } }