6

A4サイズの画像と小さい画像をファイルに保存しています。

最適なフォーマットを知りたいです。それらは、グレースケールのみのスキャンから返されます。ただし、一部の画像はサイズが非常に大きくなっています (予想と比較して)。

さまざまな品質の JPEG として保存しようとしましたが、必要なほど小さくすることはできません。JPEG は、このための最も効率的な形式ではありません。

A4 画質 8 で保存すると 196KB で保存されます 小さい画像を画質 8 で保存すると 28KB で保存されます

この画像の大部分は空白であるため、ファイル サイズがはるかに小さくなると予想していました。ここでは、画質よりもファイル サイズが重要です。私たちは何か間違ったことをしていますか?

これがサンプルのA4画像です ここに画像の説明を入力

これがサンプルの小さい画像です ここに画像の説明を入力

私たちのコードはc#で書かれています

  // Get a bitmap.
    Bitmap bmp1 = new Bitmap(@"c:\TestImageFromScanner.jpg");
    ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

    // Create an Encoder object based on the GUID 
    // for the Quality parameter category.
    System.Drawing.Imaging.Encoder myEncoder =
        System.Drawing.Imaging.Encoder.Quality;

    // Create an EncoderParameters object. 
    // An EncoderParameters object has an array of EncoderParameter 
    // objects. In this case, there is only one 
    // EncoderParameter object in the array.
    EncoderParameters myEncoderParameters = new EncoderParameters(1);

    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,8L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestJpegQuality8.jpg", jgpEncoder, myEncoderParameters);
4

4 に答える 4

1

しきい値を使用して、画像を黒または白のみに変換する関数を使用しました。これにより、画質が大幅に低下することなく、画像サイズが大幅に縮小されました。

    var downsizeImage = ImageTools.ConvertToBitonal(scaledBmp, 500);
                    downsizeImage.Save(string.Format(@"C:\Temp\{0}Downsized.png", betSlipImage.BetSlipID), ImageFormat.Png);
                    var ms = new MemoryStream();
                    downsizeImage.Save(ms, ImageFormat.Png);

   public static Bitmap ConvertToBitonal(Bitmap original, int threshold)
        {
            Bitmap source;

            // If original bitmap is not already in 32 BPP, ARGB format, then convert
            if (original.PixelFormat != PixelFormat.Format32bppArgb)
            {
                source = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
                source.SetResolution(original.HorizontalResolution, original.VerticalResolution);

                using (var g = Graphics.FromImage(source))
                {
                    g.DrawImageUnscaled(original, 0, 0);
                }
            }
            else
            {
                source = original;
            }

            // Lock source bitmap in memory
            var sourceData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            // Copy image data to binary array
            var imageSize = sourceData.Stride * sourceData.Height;
            var sourceBuffer = new byte[imageSize];
            Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize);

            // Unlock source bitmap
            source.UnlockBits(sourceData);

            // Create destination bitmap
            var destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
            destination.SetResolution(original.HorizontalResolution, original.VerticalResolution);

            // Lock destination bitmap in memory
            var destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);

            // Create destination buffer
            imageSize = destinationData.Stride * destinationData.Height;
            var destinationBuffer = new byte[imageSize];

            var sourceIndex = 0;
            var destinationIndex = 0;
            var pixelTotal = 0;
            byte destinationValue = 0;
            var pixelValue = 128;
            var height = source.Height;
            var width = source.Width;

            // Iterate lines
            for (var y = 0; y < height; y++)
            {
                sourceIndex = y * sourceData.Stride;
                destinationIndex = y * destinationData.Stride;
                destinationValue = 0;
                pixelValue = 128;

                // Iterate pixels
                for (var x = 0; x < width; x++)
                {
                    // Compute pixel brightness (i.e. total of Red, Green, and Blue values) - Thanks murx
                    //                           B                             G                              R
                    pixelTotal = sourceBuffer[sourceIndex] + sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2];
                    if (pixelTotal > threshold)
                    {
                        destinationValue += (byte)pixelValue;
                    }
                    if (pixelValue == 1)
                    {
                        destinationBuffer[destinationIndex] = destinationValue;
                        destinationIndex++;
                        destinationValue = 0;
                        pixelValue = 128;
                    }
                    else
                    {
                        pixelValue >>= 1;
                    }
                    sourceIndex += 4;
                }

                if (pixelValue != 128)
                {
                    destinationBuffer[destinationIndex] = destinationValue;
                }
            }

            // Copy binary image data to destination bitmap
            Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize);

            // Unlock destination bitmap
            destination.UnlockBits(destinationData);

            // Dispose of source if not originally supplied bitmap
            if (source != original)
            {
                source.Dispose();
            }

            // Return
            return destination;
        }
于 2013-10-23T23:15:18.073 に答える