I want to load an image from my resources to a Bitmap, and save its Pixel Format. The input image is a .bmp file with 8 BitsPerPixel image depth. This is the code:
Bitmap inputImage = new Bitmap(Test.Resources.sourceImage);
When I debug the program and I check the properties of the sourceImage, its PixelFormat is PixelFormat.Format8bppIndexed.
But after this variable assignment the PixelFormat of inputImage is Format32bppArgb.
Do you have any idea where is the problem?
var source = Test.Resources.sourceImage;
Bitmap inputImage = source.Clone(new Rectangle(Point.Empty, source.Size), source.PixelFormat);
Related
I'm currently using the below code to put an image on top of another image
Stream stream = client.OpenRead(some URL here);
Bitmap baseimg = (Bitmap)System.Drawing.Image.FromStream(stream);
Bitmap ovlimg = (Bitmap)System.Drawing.Image.FromFile("fanmade.png");
var finalImage = new Bitmap(baseimg.Width, baseimg.Height);
var graphics = Graphics.FromImage(finalImage);
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.DrawImage(baseimg, 0, 0);
graphics.DrawImage(ovlimg, 0, 0);
finalImage.Save("1.png", System.Drawing.Imaging.ImageFormat.Png);
However, the file size turns out to be a lot bigger than expected from 6-9 Mb to 16Mb. So I'm trying to find a more efficient way that would make the file size smaller while still keeping the image quality
Here are both the images
The Base IMG (Changes based on the user input but here's an example): https://pasteboard.co/K6hCXng.png
The overlay IMG: https://pasteboard.co/K6hDvIc.png
I am trying this example but I got "Source pixel format is not supported by the filter" error, and to solve it I tried with these answeres but I got titled error, then I tried to solve it with these answers.
But I am out of luck and I keep getting this error.
Can anyone give me a solution?
Heres the code:
// Open your image
string path = "sample2.jpg"; //taken from first example links initial image.
Bitmap image = (Bitmap)Bitmap.FromFile(path);
// The original bitmap with the wrong pixel format.
// You can check the pixel format with originalBmp.PixelFormat
//Bitmap originalBmp = new (Bitmap)Image.FromFile("YourFileName.gif");
// Create a blank bitmap with the same dimensions
Bitmap tempBitmap = new Bitmap(image.Width, image.Height);
// From this bitmap, the graphics can be obtained, because it has the right PixelFormat
using (Graphics g = Graphics.FromImage(tempBitmap))
{
// Draw the original bitmap onto the graphics of the new bitmap
g.DrawImage(image, 0, 0);
// Use g to do whatever you like
//g.DrawLine(...);
}
//Bitmap EditableImg = new Bitmap(image);
Bitmap a = AForge.Imaging.Image.Clone(tempBitmap, PixelFormat.Format8bppIndexed); //currently getting titled error here.
AForge.Imaging.Image.SetGrayscalePalette(a);
// create filter
DifferenceEdgeDetector filter = new DifferenceEdgeDetector();
// apply the filter
filter.ApplyInPlace(image);
error image for reference:
I want to do stamping on tiff file (Png image on tiff) but without changing its properties like DPI.
I have tried below code but it is reducing the size of tiff.
System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap) System.Drawing.Image.FromFile (inputpath);
Bitmap EditableImg = new Bitmap (bitmap);
System.Drawing.Bitmap bitmapStamping = (System.Drawing.Bitmap) System.Drawing.Image.FromFile (stamppath);
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage (EditableImg)) {
graphics.DrawImage (bitmapStamping, secondLocation);
}
EditableImg.Save (outputpath);
Problem No1.
My own Related problem
I asked the next question here
Now the Problem No 2 is.
When i am trying to open 16 Bit (Monocrome ) images from their raw pixel data
then i am getting Error.
Because i am using PixelFormat.Format16bppGrayscale on creation of Bitmap like
Bitmap bmp = new Bitmap(Img_Width, Img_Height,PixelFormat.Format16bppGrayscale);
So googled and found Format16bppGrayscale not supported so i modifed my code like below.
PixelFormat format = PixelFormat.Format16bppRgb565;
Bitmap bmp = new Bitmap(Img_Width, Img_Height, format);
Rectangle rect = new Rectangle(0, 0, Img_Width, Img_Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, format);
Marshal.Copy(rawPixel, 0, bmpData.Scan0, rawPixel.Length);
bmp.UnlockBits(bmpData);
Amazing thing is that i am getting the image now because i change the pixelFormat.
But problem is that My monocrome (Grayscale) image look in various color.
How can i get the original appearance. I tried several grayscale method but not successful
Please give me some unsafe code.
Thanks,
BobPowell's GDI+ FAQ has a bit about greyscale. The 16 bpp in .NET just doesn't work. I have to do everything in 32 bpp and resort to external tools for conversion. Luckily (?) I get to stick with TIFF images most of the time. Also, this thread might help.
You need to change the pallet to a greyscale pallet. The default pallet for 8bppIndexed is 256 colour. You can change it like this:
ColorPalette pal = myBitmap.Palette;
for (int i = 0; i < pal.Entries.Length; i++)
pal.Entries[i] = Color.FromArgb(i, i, i);
myBitmap.Palette = pal;
Alright, I have an image coming through from an external application in an 8-bit indexed format. I need this image converted to a 24-bit format of the exact same size.
I've tried creating a new Bitmap of the same size and of type Format24bppRgb and then using a Graphics object to draw the 8-bit image over it before saving it as a Bmp. This approach doesn't error out but when I open the resulting image the BMP header has all kinds of funky values. The height and width are HUGE and, in addition, there are funny (and large) values for the compression flags and a few others. Unfortunately my particular requirements are to pass this file off to a specific printer driver that demands a 24-bit image with specific header values (which I'm trying to achieve through GDI+)
Anyone know of an example on "up-converting" an indexed file to a not-indexed 24-bit file? If not an example, which path should I start down to write my own?
-Kevin Grossnicklaus
kvgros#sseinc.com
I used the code below to "up-convert" an image from 8bpp to 24bpp. Inspecting the generated 24bpp file with a hex editor and comparing against the 8bpp file shows no difference in height and width in the two files. That is, the 8bpp image was 1600x1200, and the 24bpp image has the same values.
private static void ConvertTo24(string inputFileName, string outputFileName)
{
Bitmap bmpIn = (Bitmap)Bitmap.FromFile(inputFileName);
Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(converted))
{
// Prevent DPI conversion
g.PageUnit = GraphicsUnit.Pixel
// Draw the image
g.DrawImageUnscaled(bmpIn, 0, 0);
}
converted.Save(outputFileName, ImageFormat.Bmp);
}
Everything else in the headers looks reasonable, and the images display identical on my system. What "funky values" are you seeing?
This is my conversion code. Notice the matching of resolution between source image and resulting image.
private void ConvertTo24bppPNG(Stream imageDataAsStream, out byte[] data)
{
using ( Image img = Image.FromStream(imageDataAsStream) )
{
using ( Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb) )
{
// ensure resulting image has same resolution as source image
// otherwise resulting image will appear scaled
bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
using ( Graphics gfx = Graphics.FromImage(bmp) )
{
gfx.DrawImage(img, 0, 0);
}
using ( MemoryStream ms = new MemoryStream() )
{
bmp.Save(ms, ImageFormat.Png);
data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, (int) ms.Length);
}
}
}
}
It seems odd that you're creating a Bitmap of the same width and height as your input, yet the generated BMP is much larger. Can you post some code?
The problem is probably the difference between the Vertical- and HorizontalResolution of your source image and your output image. If you load a 8bpp indexed bitmap with a resolution of 72 DPI, and then create a new 24bpp bitmap (default resolution will be 96 DPI... at least it is on my system) and then use Graphics.DrawImage to blit to the new bitmap, your image will appear slightly zoomed in and cropped.
Having said that, I don't know off the top of my head how to properly create the output Bitmap and/or Graphics object to scale properly when saved. I suspect it will have something to do with creating the images using a common scale like inches instead of pixels.