I'm trying to get information about a PNG file but I've yet to discover a comprehensive site to help me.
These are some of the semi useful code snippets I have:
Bitmap bmp = new Bitmap(pngFileName);
BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,PixelFormat.Format48bppRgb);
and
Stream imageStreamSource = new FileStream(pngFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
var decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
With these I've been able to to get the image height and width. However I still need to discover the following information:
Is it RLE encoded?
Is it in native video format?
Is it rotated?
Does it use a grayscale palette?
Does it have a transparency?
Is it RGB or BGR?
I'd really appreciate some pointers on how to acheive this or links to good articles dealing with this. We're working with .NET 4.0
I'm not sure if that helps you, but the best I've seen so far, is to walk the image pixel by pixel in a loop and accomplish your different tasks.
See these answers for examples:
Detecting if a PNG image file is a Transparent image?
Detecting grayscale images with .Net
Related
We have a system that provides images in 8-bit grayscale either tiff or jpg formats. However, the component we have to process the images expects image to be in 8-bit jpg format.
When I use .Net to save the tiff images as jpg it convets it to 24-bit image.
Is there a way, hopefully simple and fast, to convert 8-bit grayscale tiff images to equivalent jpg?
I tried and tried just to conclude that I'm sorry: .Net library's Bitmap class DOES NOT save JPEG as 8bpp even when explicitly stated and data is in grayscale.
(note: although stated in some places, JPEG format DOES support 8bpp).
At Convert an image to grayscale you may find code snipet to convert to grayscale any Image.
Using that code, I was able to save a 8bpp grayscale Image instance with '.jpeg' extension, but stating ImageFormat.Gif... that's a cheat...
My findings show as solution an entirely different approach.
The FreeImage library offers powerful APIs, including the feature needed to solve your problem.
It's home page is at http://freeimage.sourceforge.net/faq.html
But, I could not easily compile it in my Win2008 + VS 2010 machine.
One ought to sweat a lot to make it run on modern environments.
Some hints on how to accomplish that are found at http://www.sambeauvois.be/blog/2010/05/freeimage-and-x64-projects-yes-you-can/
Good luck!
Image img = Image.FromFile(filePathOriginal);
Bitmap bmp = ConvertTo8bpp(img);
EncoderParameters parameters = new EncoderParameters();
parameters.Param[0] = new EncoderParameter(Encoder.ColorDepth, 8);
bmp.Save(filePathNew, jpgCodec, parameters);
bmp.Dispose();
img.Dispose();
...
private static Bitmap ConvertTo8bpp(Image img) {
var bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
using (var gr = Graphics.FromImage(bmp))
{
gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
}
return bmp;
}
In my code, I'm receiving WriteableBitmaps from a byte array (in turn from a Kinect) and I'd like to turn them into bitmaps for use with EmguCV. Currently this is the code I have:
// Copy the pixel data from the image to a temporary array
colorFrame.CopyPixelDataTo(this.colorPixels);
// Write the pixel data into our bitmap
this.colorBitmap.WritePixels(
new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
this.colorPixels,
this.colorBitmap.PixelWidth * colorFrame.BytesPerPixel,
0);
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(colorBitmap));
MemoryStream ms = new MemoryStream();
encoder.Save(ms);
Bitmap b=new Bitmap(ms);
Image<Gray, Byte> img = new Image<Gray, Byte>(b);
img = img.ThresholdBinary(new Gray(200), new Gray(255));
I got the bottom half of the code from here.The code compiles and everything, but hangs when I'm trying to run the program (it's supposed to perform some operations on the image and then convert it back to a format that can be presented as an image.) Pausing my code and then using IntelliTrace in VS 2013, I get the following Exception at Image<Gray, Byte> img = new Image<Gray, Byte>(b); "A System.ArgumentException was thrown: URI formats are not supported." Using alternate code, from where I go directly from byte to bitmap gives me the same error. (Code can be found here.)
Anyone got tips on how to resolve this error, or alternate ways of casting to bitmap? I'm a newbie with C# & EmguCV and I'd greatly appreciate it.
Turns out all the code is fine. I'm not too sure on the technical details of the error, but the error is received when trying to write a Gray16 image in the WriteableBitmap (which is to be transformed into a Emgu Image.) Bgr565 or other formats are supported and I believe Gray16 wasn't fully implemented by MS. If doing a WinForms application, Format16bppGray will also give the same error.
I resolved to using a Gray Emgu image while writing the Bitmap as a Bgr555, which is a lot more noisy, but better than nothing.
I had the same issue. The exception with "URI formats are not supported" had nothing to do with the bitmap but with loading needed opencv dlls. I just copied the x86 and x64 folders including opencv_core290.dll and others to my executable directory.
DISCLAIMER: This code is terrible and should not be used in production. It's testing a proof of concept.
I want to create an image like below using C# the key points are the feathered edges and the transparency of the whole image.
Here is my result so far
As you can see the edges are feathered. My question is, does anyone know how to feather the edges using pure C# and maintain transparency, currently I am using a 3rd party library with some pretty nasty looking code?
Would be interested if there is a better approach to this full stop.
Here is some of the code I have been using.
Bitmap bitmap = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage(bitmap);
// Transparent Background
SolidBrush semiTransparentPen = new SolidBrush(Color.FromArgb(60, 31, 31, 31));
graphics.FillRectangle(semiTransparentPen, 0f, 0f, bitmap.Width, bitmap.Height);
// Feather edges
Bitmap bitmap1 = new Bitmap(bitmap);
Bitmap bitmap2 = new Bitmap(bitmap);
fipbmp.makeEdgesTransparentHorzSigma(bitmap1, 4, 4);
fipbmp.makeEdgesTransparentVertSigma(bitmap2, 4, 4);
fipbmp.MergeBmp(bitmap, bitmap1, bitmap2);
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);
byte[] buffer = memoryStream.ToArray();
You may find this blog post which I once wrote useful: Soft Edged Images in GDI+. That will show you a lot of what you need to know. In order to create smooth edges on an arbitrary shape you will also need a smoothing filter (which you'll then need to apply to the alpha channel), which you can read about here.
I am using Bit Miracle LibTiff.Net.
I cannot find any sample code to take a 32bpp ARGB colour image and write the bitmap to a TIFF using this library.
Has anyone else attempted this?
Here is my sample code. It produces a file, but it cannot be viewed by any software that I have.
EDIT: The code now works, but the colors are wrong!
public static void WriteTiff(Image image, string fileName)
{
Bitmap target = image as Bitmap;
BitmapData bmd = target.LockBits(
target.GetRectangle(),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
var bits = new byte[bmd.Stride * bmd.Height];
Marshal.Copy(bmd.Scan0, bits, 0, bits.Length);
target.UnlockBits(bmd);
Tiff tiff = Tiff.Open(fileName, "w");
tiff.SetField(TiffTag.IMAGEWIDTH, target.Width);
tiff.SetField(TiffTag.IMAGELENGTH, target.Height);
tiff.SetField(TiffTag.COMPRESSION, Compression.NONE);
tiff.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
tiff.SetField(TiffTag.BITSPERSAMPLE, 8);
tiff.SetField(TiffTag.SAMPLESPERPIXEL, 4);
tiff.SetField(TiffTag.ROWSPERSTRIP, target.Height);
tiff.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
tiff.WriteEncodedStrip(0, bits, bits.Length);
tiff.Close();
}
Thanks
EDIT
The latest build of LibTiff.Net contains samples for conversion of a System.Drawing.Bitmap to 32-bit or 24-bit color LZW compressed TIFF images.
There is also samples for conversion of a TIFF image to 32-bit or 24-bit System.Drawing.Bitmaps.
/EDIT
You may also want to review "Graphics programming with LibTiff.Net" (part 1, part 2) article
in documentation. It should give you basic information about creating
color TIFF files.
Disclaimer: I am one of the maintainers of the library.
I am writing a program that resizes pictures like this:
Image originalImage = Image.FromFile(pathToOriginalPicture);
Bitmap b = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage(b);
g.DrawImage(originalImage, 0, 0, newWidth, newHeight);
g.Dispose();
b.Save(pathToOutputPicture, ImageFormat.Jpeg);
I tried to set:
newWidth = originalImage.Width;
newHeight = originalImage.Height;
The result was that the rezised picture file became ~900K while the original file was ~4M.
Why this is happening ?
Is the quality of the original picture better than the resized one ? How?
I opened both pictures in Photoshop and I see that the original picture was 72ppi, while the resized one became 96ppi. Why is that ? Can I control this ?
Thanks a lot for your time !
You're not telling us the original format of your picture but you're saving as a JPEG:
b.Save(pathToOutputPicture, ImageFormat.Jpeg);
JPEG is a lossy compression format.
In addition to being lossy, JPEG also can output different quality (which is configurable).
This is what is happening to your file size: it is shrinking because you went, say, from a lossless format to the lossy JPEG or because you went from JPEG to JPEG-with-a-lower-quality.
Hence the size reduction.
Besides the format you need to set DPI, compression level settings etc. Check your Save function for overloads that will accept this type of input. See this documentation.