スキップしてメイン コンテンツに移動

投稿

ラベル(png)が付いた投稿を表示しています

C#: Create Indexed PNG Using System.Windows.Media.BitmapSource with Pre-defined Palettes in BitmapPalettes Class

Creating Indexed PNG in C# In C#, there are mainly two namespaces to creating image... System.Drawing namespace System.Windows.Media namespace I've already written the post for creating indexed png using System.Drawing namespace classes - Create Indexed PNG Using C# .Net . So, in this post, I will show you how to create indexed png using classes under System.Windows.Media namespace. Preparation for Using Classes Under System.Windows.Media You need to add following dlls to your project reference in order to handle images using classes under System.Windows.Media. PresentationCore.dll Xaml.dll WindowsBase.dll Creating Palette Based Indexed Image If you would like to create indexed png, first of all, you should create color palette which can hold max 256 colors. Creating color palette is trivial. Please see the below example code: var colors = new List<Color>(); colors.Add(Color.FromRgb(255, 0, 0)); // 0 - red colors.Add(Color.FromRgb(0, 255, 0)); // 1 -

C#: Create Indexed PNG Image Using pngcs Library

I have written C# program which create an indexed packed png image from a given png image using pngcs 1_1_4. What you need to use pngcs is adding reference of dll to your Visual Studio project - Pngcs.dll and ICSharpCode.SharpZipLib.dll for .Net 2.0, and Pngcs45.dll for .Net4.5. I have used .Net 2.0 version because my Visual Studio is 2010. I have already written a program for generating indexed png in C# on this post - Create Indexed PNG Using C# .Net . But the png encoder Microsoft officially provides does not support compression level parameter. That is, even if png is indexed, the size of png image is not sufficient for me :( So I investigated other library which can handle png in C#, and finally found pncs! Main features of my program: Use minimum bit per pixel as much as possible Image which has alpha channel is is not supported because pngcs library doesn't support color palette with alpha channel If given image has alpha channel but all alpha channel value is 25

Create Indexed PNG Using C# .Net

Currently I am working on optimizing image size. In this post, I will demonstrate the C# code for creating indexed png image. The code might not be perfect yet, but hope this code will give you some hints ;) A few things I am struggling with while I am writing the example code: Palette: you should take ColorPalette from Bitmap, modify the ColorPalette object and set buck it to the Bitmap object. BitmapData.Stride is often bigger than BitmapData.Width. You should be careful to check both values difference when you manipulating the image array index. The example code uses classes in System.Drawing namespace. But looks there is another option to manipulate image in C# .Net - System.Windows.Media namespace. C# Source Code for Creating Indexed PNG The following implementation of CreateIndexedPng method is able to take only non-indexed image like Format24bppRgb or Format32bppArgb. (I might add other image formats in future.) If the source image has only 2 colors, we can use For

Java: Create Index PNG Using PNGJ 2.1.1

Introduction I have written java code for generating indexed png using pngj version 0.94. Now pngj version 2.1.1 has been released on 2014/6/1. So I have refined the code for generating indexed png so that the code suits with pngj version 2.1.1. Code package com.dukesoftware.utils.image.pngj; import ar.com.hjg.pngj.IImageLine; import ar.com.hjg.pngj.ImageInfo; import ar.com.hjg.pngj.ImageLineInt; import ar.com.hjg.pngj.PngReader; import ar.com.hjg.pngj.PngWriter; import ar.com.hjg.pngj.chunks.ChunkCopyBehaviour; import ar.com.hjg.pngj.chunks.PngChunkSingle; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public final class PngUtils { private static final int _1K_BYTES = 1024; public static void toIndexed256Colors(File input, File output) throws IOException { PngReader png1 = new PngReader(input); ImageInfo pnginfo1 = png1.imgInf

Java: Create Indexed PNG Image Using Standard Java Image API

Hello guys! I have written Java: How to Create Indexed PNG Using PNGJ Library a few years ago. Recently I investigated how to create indexed png by standard Java image API. I have googled and tested various methods, finally I figureed out how to create indexed png image. Still my program is not perfect (of course most of the case it worked well), I believe that this program gives you the hint to creating indexed png and image manipulation on standard Java API. Code Actually the method is quite straigt forward. Please see the code below :) The advantedge of this code is the code doesn't require any library. The code below contains all of the stuffs needed for creating indexed png image! package com.dukesoftware.util.image; import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; import java.awt.image.ColorConvertOp; import java.awt.image.ColorModel; import java.awt.image.DataBuffer; import java.awt.image.IndexColorModel; import java.io.File; import java.

Java: How to Create Indexed PNG Using PNGJ Library

I'm working on Google App Engine for Java (GAEJ) now. Very excited about working on it because can develop Java Web App quite easliy & quickly! However I faced a limitation of image processing on GAEJ. On GAEJ, java.awt pakage.* is not supported. That means we cannot use BufferedImage etc!! As you know, Google provides com.google.appengine.api.images.* for image processing on GAEJ, something like below: ImagesService imagesService = ImagesServiceFactory.getImagesService(); Image srcImage = ImagesServiceFactory.makeImage(srcImageData); // some transformations. Transform crop = ImagesServiceFactory.makeCrop(0.3, 0, 1, 0.70); OutputSettings settings = new OutputSettings(OutputEncoding.PNG); // apply transform Image newImage = imagesService.applyTransform(crop, srcImage, settings); byte[] newImageData = newImage.getImageData(); It can read jpg, png, gif images accroding to the official document. And if transformation is applied, the output binary data becomes png forma

ActionScript 3.0: Save BitmapData As JPEG or PNG

public static function saveBitmapDataAsJPEG(path:String, bitmapData:BitmapData, quality:Number=50.0):void { saveByteData(new JPEGEncoder(quality).encode(bitmapData), path); } public static function saveBitmapDataAsJPEGAsync(path:String, bitmapData:BitmapData, quality:Number=50.0):void { saveByteDataAsync(new JPEGEncoder(quality).encode(bitmapData), path); } public static function saveBitmapDataAsPNG(path:String, bitmapData:BitmapData):void { saveByteData(new PNGEncoder().encode(bitmapData), path); } public static function saveBitmapDataAsPNGAsync(path:String, bitmapData:BitmapData):void { saveByteDataAsync(new PNGEncoder().encode(bitmapData), path); } Here is my IO Utility methods. public static function saveByteData(data:ByteArray, path:String):void { try { var file:File = new File(path); var fs:FileStream = new FileStream(); fs.open(file, FileMode.WRITE); fs.writeBytes(data); fs.close(); } catch (err:IOError) { trace(err); } } public static function saveB

C#: Save Image as JPEG or PNG format

Just a simple code tips for saving image as JPEG or PNG format in C#. Save image as JPEG with Quality parameter public static readonly ImageCodecInfo JPEG_CODEC = GetEncoderInfo("image/jpeg"); public static void SaveImageAsJpeg(this Image src, string fileName, int quality) { var eps = new EncoderParameters(1); var ep = new EncoderParameter(Encoder.Quality, quality); eps.Param[0] = ep; src.Save(fileName.ChangeExtension("jpg"), JPEG_CODEC, eps); } public static ImageCodecInfo GetEncoderInfo(string mineType) { return ImageCodecInfo.GetImageEncoders().First(enc => enc.MimeType == mineType); } Save image as PNG public static void SaveImageAsPng(this Image src, string fileName) { src.Save(fileName.ChangeExtension("png"), ImageFormat.Png); }

ActionScript 3.0: Load SWF, JPG, PNG

Hey guys! I wrote small utility method for loading swf, jpg, png etc. in ActionScript 3.0. public static function loadFromURL(url:String):Loader { var img:Loader = new Loader(); img.load(new URLRequest(url)); return img; } public static function loadFromFile(path:String):Loader { var img:Loader = new Loader(); img.loadBytes(IOUtils.readByteData(path)); return img; } public static function load(url:String, format:String):URLLoader { var loader:URLLoader = new URLLoader(); loader.dataFormat = format; loader.addEventListener(Event.COMPLETE, completeHandler); loader.load(new URLRequest(url)); function completeHandler(event:Event):void { loader.removeEventListener(Event.COMPLETE, completeHandler); //trace(event.target.data); } return loader; }