[Turn] c# drawing in the bitmap class to process the picture, the storage precautions

Today, I look for the drawing program I wrote before, I want to improve it, and I found

When adding a text watermark to the picture, it will be a little blurry, especially when the small font is very blurred.

After some adjustment, finally found the problem

Post the code to warn yourself

   System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("b1.jpg"));
        Bitmap bitWaterMark = new Bitmap(image.Width, image.Height);
        Graphics g = Graphics.FromImage(bitWaterMark);

        g.DrawImage(image, 0, 0, image.Width, image.Height);

        Font f = new Font("verdana", 14);
        Brush c = new SolidBrush(Color.FromName("red"));
        string addtext = "测试字体啊";
        g.DrawString(addtext, f, c, 10, 1);
        g.Dispose();
        image.Dispose();
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        bitWaterMark.Save(ms, ImageFormat.Bmp);

   //bitWaterMark.Save(ms, ImageFormat.Jpeg);
        //Regenerate the Image object 
        System.Drawing.Image img2 = System.Drawing.Image.FromStream(ms);
        //Return a new Image object 
        //bitWaterMark.Save( Server.MapPath("test.jpg"));
        img2.Save(Server.MapPath("test.jpg"));

      //img2.Save(Server.MapPath("test.jpg"), ImageFormat.Jpeg);
        bitWaterMark.Dispose();

The red font part is the problem. I only found this problem today. I think friends who use C# to draw will also find this problem.

When using the bitemap class to process the image object, whether it is stored to a file or stored as a data stream, if you add ImageFormat.Jpeg

It will make the picture blurry, which is really weird. If it must be stored as a data stream, then use ImageFormat.Bmp to store and save it, and the generated picture will be much clearer than ImageFormat.Jpeg, but the problem is finally solved, the generated picture Very clear.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325174181&siteId=291194637