Image manipulation is fun when it gets working. So far I am able to convert 24 bpp bitmap images to 8 bpp and do some tricks.
But I am stuck can't change them to 1 bpp which is exactly what I want.
I work on .Net4 and I use an external library called Aforge.
does any body know how to convert an 8 bpp image to 1 bpp?
thanks
Good question, introduced me to a great library :)
For solution:
Bitmap binary = new Bitmap( 100, 100, PixelFormat.Format1bppIndexed );
As found at:
http://www.aforgenet.com/forum/viewtopic.php?f=2&t=1502
You can use some image processing techniques such as Binarize and IntensityDetect to convert 8-bit images to black and white.
We searched the internet and found some commercial toolkits that support these functions, but they don't support many file formats. We finally found a toolkit named leadtools that supports these features with huge set of other images processing functions. I noticed that this toolkit can natively work with any bit depths.
After a long search, I got this code example working.
Bitonal (TIFF) Image Converter for .NET
Related
I have an image https://drive.google.com/file/d/16Xotc-2CJ6HkEJDysfKBkjClkU1OGiyQ/view?usp=sharing that is GrayScale but every library I have tried, ImageMagick, ImageSharp, System.Drawing seem to interpret it as black and white, but when you open it in ImageJ or Photoshop or Incarta or many other software you can clearly see it is grayscale.
can anyone help me find a way to display this image? here is something I've tried but i've tried almost a dozen different things
TiffEncoder encoder = new TiffEncoder();
encoder.PhotometricInterpretation = SixLabors.ImageSharp.Formats.Tiff.Constants.TiffPhotometricInterpretation.BlackIsZero;
SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(mysteryTiff);
PixelTypeInfo pixType = image.PixelType;
// Stretches the image to fit the pictureBox.
Stream stream = new MemoryStream();
image.SaveAsTiff(stream, encoder);
stream.Position = 0;
MagickImage magickImage = new MagickImage(stream);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.ClientSize = new System.Drawing.Size(1200, 1200);
pictureBox1.Image = magickImage.ToBitmap();
Can anyone display this image correctly. It will display correctly when uploaded to
What you have there, according to the image tag directory, is a 2024x2024 16-bpp greyscale LZW-compressed extended TIFF. It even opens in some software, which proves that it's not malformed. So far so good.
Now here's where it breaks down: 16-bpp greyscale is not supported by a lot of things. The 'why' is mildly convoluted, having to do largely with "but we all use 8 bits per channel, and so does the hardware, so why bother", but the end result isn't: if you want to use anything above 8 bits per channel, you'll either have to find something that will do the work for you or convert the data to 8-bpp at some point.
Even when the file format explicitly support 16-bpp greyscale (TIFF and PNG for instance), most libraries tend not to support either read or write in that format because it is so rarely used that they don't bother to implement it. I ended up writing my own PNG encoder for 16-bpp greyscale images (converted from 12-bpp and 16-bpp XRAY images), but the images aren't viewable in most programs that supposedly support the full PNG standard.
In this case your best option is probably going to be to write a conversion of your own for this type of file. Assuming that the same format (16-bpp, LZW-compressed) is produced by the source application every time, it shouldn't be too difficult to convert the pixel buffer to 8-bpp and save out as TIFF, PNG or whatever you like. You'll lose half of your greyscale (depth) resolution, but for display purposes they're not going to help much anyway. It only really matters when there's a good reason to retain the full range of values.
Using SFML, I can render about any picture format using the following code:
SFML.Graphics.Image img = new SFML.Graphics.Image("pic.bmp");
SFML.Graphics.Texture tex = new Texture(img);
SFML.Graphics.Sprite sprite = new Sprite(tex);
renderWindow.Draw(sprite);
But this is not working correctly with a few bitmaps (files with .bmp extension) which are formatted in 8 bits (256 colors). It looks like the SFML lib is attempting to read it in 16 or 32 bits.
Is there something to fix this situation?
Update: I have opened problematic bmp files with paint and just saved them in the same format (8bits), then SFML became able to display those properly. But this is only a test and I can't do it using paint, I need a code-only solution.
If resaving the files again in a different application fixes the issue, then this sounds to me like you have a special format that's not supported by stb_image and thus isn't supported by SFML.
I highly recommend to use the PNG format, as it supports proper transparency and uses a loss-less compression.
I am working on a project that at it's core involves adding text to an image, so as an example given a background image (B) and some text in a specified font, point size and font (A) the two are composited together to produce (C):
The eventual result is to go to print with these images, so the backgrounds are using the CMYK Color Space and I need to keep the whole process within CMYK or the colors look wrong when printed. (note: excellent article on Color Spaces and .NET on CodeProject)
I have tried several different ways of compositing these images together:
System.Drawing implicitly converts everything to RGB
System.Windows.Media.Imaging - no compositing methods
System.XAML/WPF - very promising however RenderTargetBitmap does not work in PixelFormats.Cmyk32 (throws an ArgumentException).
I have looked at but not tried third party commercial components as the prices seem to start high and continue going up:
Graphics Mill (~US$1800 as of 12/2010)
Atalasoft DotImage (~US$3300 as of 12/2010)
Is this possible in .NET 4?
Edit:
Because someone else might want to do something slightly different and just convert any format that Windows.System.Media.Imaging is able to load to CMYK here is the code I have used:
var bitmapConverter = new FormatConvertedBitmap();
bitmapConverter.BeginInit();
bitmapConverter.Source = sourceImage;
bitmapConverter.DestinationFormat = PixelFormats.Cmyk32;
bitmapConverter.EndInit();
To clarify the above code converts an image source to CMYK32 (no transparency) however if you are using certain classes (namely RenderTargetBitmap passing the above ImageSource will throw an exception).
If you have looked much on SO, you have probably already seen these links. But, just in case, here are some links that I found that might be helpful to you.
Here is a link from here on SO about working with CMYK in .NET:
Convert RGB color to CMYK?
Specifically it mentions using Color Profiles and Windows Color Management APIs.
Here is another SO link:
How to convert CMYK to RGB programmatically in indesign
One answerer mentions that there is not an exact conversion between CMYK and RGB.
Here is a link about compositing two CMYK images without converting to RGB:
The are any CMYK graphics library?
The answerer suggests using a commercial product that he is affiliated with.
I am attempting to open .tif files that have color in them (300 dpi, PixelFormat.Format24bppRgb) using the .Net Image and Bitmap classes. I always get an "invalid parameter" error when the tiffs have color (works fine for black and white tiffs). If anyone has source code on how to open a .tif with color in it I'd deeply appreciate it. Below is what I'm attempting to do; this also fails when calling Bitmap.FromStream:
using (FileStream fs = File.OpenRead(fileName))
{
using (Image img = Image.FromStream(fs)) {}
}
The reason why you can't open the image file is that the image maybe has compression format.
I also encounter this problem when I open a .gif image file.
I'm searching the solutions.
We can communicate each other.
showlie#163.com
thanks
I think the basic problem is that one cannot use the .Net Image/Bitmap classes to reliably open color TIFFs. Many of the various compression formats used to encode color TIFFs break the .Net Image/Bitmap class.
It seems to be a catch 22--you have to know the TIFF formatting information to know how to load it, but in .Net one needs to load the Image/Bitmap class to read TIFF formatting information.
I think the answer to my question is, "this can't be done just using the .Net framework," and that I'll need to either buy a 3rd party control or create something myself in C++.
I found the GDI(+) the limiting factor in dealing with TIFF. I had many "invalid parameter" issues on XP (in my case because of using 16bpp grayscale and tiled tiff). The same code worked flawlessly on Windows7/64bit.
For me, the free LibTIFF.NET was the solution. (StackOverflow using LibTIFF from c#) Only con compared to GDI+: you'll have to roll your own BMP (uncompressed byte array to bmp conversion). I can provide you with examples if you like.
To diagnose your TIFF file type, you could use TiffDump download link
Using like tiffdump myfile.tif will produce an output like:
Magic: 0x4949 <little-endian> Version: 0x2a
Directory 0: offset 1669857288 (0x63880008) next 0 (0)
ImageWidth (256) SHORT (3) 1<33227>
ImageLength (257) SHORT (3) 1<24986>
BitsPerSample (258) SHORT (3) 1<16>
Compression (259) SHORT (3) 1<1>
Photometric (262) SHORT (3) 1<1>
FillOrder (266) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<1>
XResolution (282) RATIONAL (5) 1<6400>
YResolution (283) RATIONAL (5) 1<6400>
PlanarConfig (284) SHORT (3) 1<1>
ResolutionUnit (296) SHORT (3) 1<2>
TileWidth (322) SHORT (3) 1<256>
TileLength (323) SHORT (3) 1<256>
Most likely such info can get you more specifc answers on StackOverflow.
I'm loading a Bitmap from a jpg file. If the image is not 24bit RGB, I'd like to convert it. The conversion should be fairly fast. The images I'm loading are up to huge (9000*9000 pixel with a compressed size of 40-50MB). How can this be done?
Btw: I don't want to use any external libraries if possible. But if you know of an open source utility class performing the most common imaging tasks, I'd be happy to hear about it. Thanks in advance.
The jpeg should start with 0xFF 0xD8. After that you will find various fields in the format:
Field identifier 2 bytes
Field length, excluding field identifier. 2 bytes.
Variable data.
Parse through the fields. The identifier you will be looking for is 0xFF 0xC0. This is called SOF0, and contains height, width, bit depth, etc. 0xFF 0xC0 will be followed by two bytes for the field length. Immediately following that will be a single byte showing the bit depth, which will usually be 8. Then there will be two bytes for height, two for width, and a single byte for the number of components; this will usually be 1 (for greyscale) or 3. (for color)
This isn't something I've tried myself, but I think you might need to acccess the picture's EXIF information as a start.
Check out Scott Hanselman's blog-entry on accessing EXIF information from pictures.
Standard .NET System.Drawing namespace should have all that you need,
but it probably won't be very efficient. It'll load the whole thing into RAM, uncompress it, convert it (probably by making a copy) and then re-compress and save it. If you aim for high performance, I'm afraid you might need to look into C/C++ libraries and make .NET wrappers for them.
As far as I know jpg is always 24 bpp. The only thing that could change would be that it's CMY(K?) rather then RGB. That information would be stored in the header. Unfortunately I don't have any means of creating a CMYK image to test whether loading into a Bitmap will convert it automatically.
The following line will read the file into memory:
Bitmap image = Image.FromFile(fileName);
image.PixelFormat will tell you the image format. However, I can't test what the file load does with files other than 24bpp RGB jpgs. I can only recommend that you try it out.