Skip to content

Instantly share code, notes, and snippets.

@TonyMooori
Last active August 22, 2017 15:03
Show Gist options
  • Save TonyMooori/48d5b2fe9504e7666fad2ef6b02bb328 to your computer and use it in GitHub Desktop.
Save TonyMooori/48d5b2fe9504e7666fad2ef6b02bb328 to your computer and use it in GitHub Desktop.
JPEGでコピーしまくって画像を劣化させるプログラムです
using System;
using System.Drawing;
class Program
{
static void Main()
{
Console.WriteLine("input file path");
var input = Console.ReadLine();
Console.WriteLine("input the number of copying");
var n = int.Parse(Console.ReadLine());
var img1 = new Bitmap(input);
img1.Save("temp.jpg");
for(int i = 0 ; i<n ; i++ )
{
var temp1 = new Bitmap("temp.jpg");
var temp = new Bitmap(temp1);
if( i%(n/10) == 0 ){
temp.Save(i.ToString()+".png");
}
temp1.Dispose();
// https://dobon.net/vb/dotnet/graphics/encoderparameters.html
var eps = new System.Drawing.Imaging.EncoderParameters(1);
var ep = new System.Drawing.Imaging.EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, (long)100-i*100/n);
//Console.WriteLine((long)100-i*100/n);
eps.Param[0] = ep;
var ici = GetEncoderInfo("image/jpeg");
temp.Save("temp.jpg",ici,eps);
temp.Dispose();
}
}
//MimeTypeで指定されたImageCodecInfoを探して返す
private static System.Drawing.Imaging.ImageCodecInfo
GetEncoderInfo(string mineType)
{
//GDI+ に組み込まれたイメージ エンコーダに関する情報をすべて取得
System.Drawing.Imaging.ImageCodecInfo[] encs =
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
//指定されたMimeTypeを探して見つかれば返す
foreach (System.Drawing.Imaging.ImageCodecInfo enc in encs)
{
if (enc.MimeType == mineType)
{
return enc;
}
}
return null;
}
//ImageFormatで指定されたImageCodecInfoを探して返す
private static System.Drawing.Imaging.ImageCodecInfo
GetEncoderInfo(System.Drawing.Imaging.ImageFormat f)
{
System.Drawing.Imaging.ImageCodecInfo[] encs =
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
foreach (System.Drawing.Imaging.ImageCodecInfo enc in encs)
{
if (enc.FormatID == f.Guid)
{
return enc;
}
}
return null;
}
}
/*
DSC_2122.jpg
animal_ratel.png
1001
1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment