Bitmap Save to .Net Save image in wrong format?

We have a piece of code that saves the .Net System.Drawing.Bitmap file to a file. The Save call indicates the location of the file, as well as ImageFormat, which we expect to save image data in the form of Jpeg code as follows:

public MediaFile IngestImage(System.Drawing.Bitmap imgSrc, string name){
     ... // left out because it is not relevant to this question
     imgSrc.Save(fullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
     ... // left out because it is not relevant to this question
}

For some reason, this method occasionally generates PNG images as .jpg files. In most cases, this is not a big problem, but in another part of the project there are problems with the fact that these files are not actual jpeg (Windows Media Services).

Any help is appreciated, has anyone ever seen this?

Note: the full path is something like "\ servcer \ share \ file.jpg". We save jpg with the extension "jpg". Therefore, the problem ... Later, we create publishing points on Windows Media Server to play the SMIL playlist, after which we must “Declare” files and formats at the publishing point, when the publishing point starts playing it, expects the Jpg file to be a file extension , and the content is actually PNG

Here is the actual code creating the BitpMap object that is passed to the above method ...

        public static Bitmap CreateBitmap(string text, int height, int width, Color foregroundColor, Color backgroundColor, string fontName, int fontSize, bool antialias)
    {
        // Initialize graphics
        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            if (antialias)
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
            }

            // Set colors
            SolidBrush fgBrush = new SolidBrush(foregroundColor);
            SolidBrush bgBrush = new SolidBrush(backgroundColor);

            // paint background
            RectangleF rectF = new RectangleF(0, 0, width, height);
            g.FillRectangle(bgBrush, rectF);

            // Load font
            FontFamily fontFamily = FontFamily.GenericSerif;
            Font font = new Font(fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
            try
            {
                fontFamily = new FontFamily(fontName);
                font = new Font(fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
            }
            catch { }

            // Set font direction & alignment
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            // Finally, draw the text
            g.DrawString(text, font, fgBrush, rectF, format);

            return bmp;
        }
    }
+3
source share
8 answers

I would approach this:

  • CreateBitmap(). , , , , new return? , , . , , , .

  • Save try/catch, , - ExternalException. Bitmap.Save() ( Image.Save()) ExternalException:

.

- -

.

, fullPath PNG .jpg?

+3

. - JPG. ImageFormat, PNG JPG. Photoshop " JPEG". .png, Photoshop.

JPG JPG.

, . , .

+2

, . , :

    Random r = new Random();
    for (int i = 0; i < max; i++)
    {
        System.Diagnostics.Debugger.Break()
        int size = 1 + i;
        int width = 1 + i;
        SaveBitmap(@"C:\Projects\test.jpg", ImageFormat.Jpeg, "hello", size, width, Color.Black, Color.White, "Arial", size, true);
        SaveBitmap(@"C:\Projects\test.png", ImageFormat.Jpeg, "hello", size, width, Color.Black, Color.White, "Arial", size, true);
        SaveBitmap(@"C:\Projects\test.jpg", ImageFormat.Jpeg, "hello", size, width, Color.Black, Color.White, "Arial", size, false);
        SaveBitmap(@"C:\Projects\test.png", ImageFormat.Jpeg, "hello", size, width, Color.Black, Color.White, "Arial", size, false);
    }

    public static void SaveBitmap(string filename, ImageFormat format, string text, int height, int width, Color foregroundColor, Color backgroundColor, string fontName, int fontSize, bool antialias)
    {
        using (Image source = CreateBitmap(text, height, width, foregroundColor, backgroundColor, fontName, fontSize, antialias))
            source.Save(filename, format);

        using (Image imgLoaded = Bitmap.FromFile(filename))
        {
            if (imgLoaded.RawFormat.Guid != format.Guid)
                throw new InvalidOperationException();
        }
    }

if (imgLoaded.RawFormat.Guid!= format.Guid) . , , CreateBitmap.

:

SaveBitmap() Image.Save(). , , ...

+1

, , . , .png, jpegformat PNG.

0

ImageCodecInfo ?

Dim enc = ImageCodecInfo.GetImageEncoders().ToList().Find(Function(e) e.MimeType = "image/png")
objBitMap.Save(stream, enc, params)
0

,

public bool ResizeImage(string OriginalFilepath, string NewFilepath)
    {
        Bitmap original = (Bitmap)Image.FromFile(OriginalFilepath);
        Bitmap resized = new Bitmap(original, new Size(width,height));
        resized.Save(NewFilepath.jpeg);

    }
0

# Win7 (64 ) VS 2010 Bitmap.Save( "wnd.bmp" ) png . MainWnd - # Form capture .

        Bitmap bmp = new Bitmap(MainWnd.ActiveForm.Width, MainWnd.ActiveForm.Height);
        Rectangle crec = MainWnd.ActiveForm.ClientRectangle;
        Rectangle r = this.ClientRectangle;
        MainWnd.ActiveForm.DrawToBitmap(bmp, r);
        bmp.Save("wnd.bmp");  // saves as PNG not BMP !!

00000000 89 50 4E 47 0D 0A 1A 0A - 00 00 00 0D 49 48 44 52 ‰ PNG........ IHDR 00000010 00 00 04 0B 00 00 02 E6 - 08 06 00 00 00 24 30 1D....... æ..... $0. 00000020 D1 00 00 00 01 73 52 47 - 42 00 AE CE 1C E9 00 00 Ñ.... sRGB.®Î.é..

0

, fullPath png, imgSrc Jpeg.

, fullPath jpeg jpg, png.

msdn doc Save.

-1

Source: https://habr.com/ru/post/1716321/


All Articles