[C#]処理ビデオ


1.WPFのシステム.Windows.Controls.イメージファイルをイメージに読み込みます.
次の方法は、プロセスが開いてすぐにファイルを閉じることです.
//System.Windows.Controls.Image img_main;
byte[] buffer = System.IO.File.ReadAllBytes(@"file.jpg");
MemoryStream ms = new MemoryStream(buffer);
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.CacheOption = BitmapCacheOption.OnLoad;
bmi.StreamSource = ms;
bmi.EndInit();
img_main.Source = bmi;
逆に、この方法のプロセスはファイルを処理しています.そのため、この方法は使用しないことをお勧めします.
//Do not use this method
BitmapImage img = new BitmapImage(new Uri(@"file.jpg"));
Image.Source = img;
2.WinFormのPictureBox.イメージファイルをイメージに読み込みます.
次の方法は、プロセスがファイルをキャプチャしていないことです.
//PictureBox pic_img;
FileStream fs = new FileStream(@"file.jpg", FileMode.Open, FileAccess.Read);
pic_img.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
この方法では、プロセスがファイルの処理を続行できます.お勧めします.
//Do not use this method
//PictureBox pic_img;
pic_img.Image = System.Drawing.Image.FromFile(@"file.jpg")
3.PictureBoxにリソース内のイメージを読み込む
//PictureBox pic_img;
object O = Resources.ResourceManager.GetObject("resource name");
pic_img.Image = (Image)O;
(deprecated). System.Drawing.Bitmapをbyte[]イメージに変換します.
まず、ファイルからビットマップをロードすると、次のように処理せずにファイルが開きます.
FileStream fs = new FileStream(@"file.jpg", FileMode.Open, FileAccess.Read);
System.Drawing.Bitmap bm=(Bitmap)System.Drawing.Image.FromStream(fs);
fs.Close();
byte[]画像に変換する場合はstepを削除することをお勧めします.widthとheightは変わらない.
以下の方法は一時的な方法であり、実際には画像フォーマットに基づいてタイトルのサイズを正確に切り取るべきであり、以下の方法はタイトルのサイズが画像の高さより小さいと仮定した上で作成されるコードである.タイトルが画像の高さより大きい場合はどうなりますか?
byte[] Bitmap2Bytes(Bitmap bm)
{
    //Bitmap은 상하가 반전되어 있으므로 flip 시킨다.
    bm.RotateFlip(RotateFlipType.RotateNoneFlipY);
    System.IO.MemoryStream stream = new System.IO.MemoryStream();
    bm.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
    byte[] bytes = stream.ToArray();
    byte[] newBytes = new byte[bm.Width * bm.Height * 3];
    
    int rem = (bytes.Length - (bm.Width * bm.Height * 3));  //쓸모없는 바이트 수
    int header = rem % bm.Height;   //bitmap header의 크기
    int stride = rem / bm.Height;  
    for (int h = 0; h < bm.Height; h++)
    {
        int line = header + h * (bm.Width * 3 + stride);
        for (int w = 0; w < bm.Width * 3; w += 3)
        {
            newBytes[h * bm.Width * 3 + w + 0] = bytes[line + w + 0];
            newBytes[h * bm.Width * 3 + w + 1] = bytes[line + w + 1];
            newBytes[h * bm.Width * 3 + w + 2] = bytes[line + w + 2];
        }
    }
    return newBytes;
}