当前位置: 首页>>代码示例>>C#>>正文


C# Bitmap.ToByteArray方法代码示例

本文整理汇总了C#中System.Drawing.Bitmap.ToByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.ToByteArray方法的具体用法?C# Bitmap.ToByteArray怎么用?C# Bitmap.ToByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Drawing.Bitmap的用法示例。


在下文中一共展示了Bitmap.ToByteArray方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Crop

        /// <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();
        }
开发者ID:rcarubbi,项目名称:Carubbi.Components,代码行数:19,代码来源:BitmapExtensions.cs

示例2: GetNewBitmapBytes

        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;
        }
开发者ID:thorium227,项目名称:TextPictureMap,代码行数:13,代码来源:BitmapTextConverter.cs

示例3: ProcessAsset

        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;
        }
开发者ID:MrHayato,项目名称:PhotoCache,代码行数:13,代码来源:AssetModule.cs

示例4: CreateCache

        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);
        }
开发者ID:MrHayato,项目名称:PhotoCache,代码行数:37,代码来源:CacheModule.cs

示例5: Publish

 /// <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());
//.........这里部分代码省略.........
开发者ID:hephaistosthemaker,项目名称:mangarack,代码行数:101,代码来源:Publisher.cs

示例6: Publish

 /// <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();
         }
     }
 }
开发者ID:Kokoro87,项目名称:mangarack.cs,代码行数:90,代码来源:Publisher.cs


注:本文中的System.Drawing.Bitmap.ToByteArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。