close
C#媒體影音--轉換影像大小

System.Drawing.Bitmap img1 = new System.Drawing.Bitmap(@"C:/pic/original.jpg");           
System.Drawing.Bitmap img2 = new System.Drawing.Bitmap(img1.Width *2, img1.Height*2 );

System.Drawing.Imaging.BitmapData bmData = img1.LockBits(new System.Drawing.Rectangle(0, 0, img1.Width , img1.Height ), System.Drawing.Imaging.ImageLockMode.ReadWrite, img1.PixelFormat);        
System.Drawing.Imaging.BitmapData bmData2 = img2.LockBits(new System.Drawing.Rectangle(0, 0, img2.Width, img2.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, img1.PixelFormat);  
       
int stride = bmData.Stride;
int stride2 = bmData2.Stride;
unsafe
{
    int nOffset = stride – img1.Width * 3;
    int nOffset2 = stride2 – img2.Width * 3;
    System.IntPtr t = bmData.Scan0;
    System.IntPtr t2 = bmData2.Scan0;
    byte* p = (byte*)(void*)t;                                  
    byte* p2 = (byte*)(void*)t2;

    for (int y = 0; y < img2.Height; y++)
    {  
        for (int x = 0; x < img2.Width; x++)
        {
             p2[y * (img2.Width * 3 + nOffset2) + (x * 3) + 0] = (byte) (255);     //blue 底色
        }
    }

    for (int y = 0; y < img1.Height; y++)
    {
        for (int x = 0; x < img1.Width ; x++)
        {   
            p2[y * (img2.Width * 3 + nOffset2) + (x * 3) + 0] = p[y * (img1.Width * 3 + nOffset) + (x * 3) + 0];
            p2[y * (img2.Width * 3 + nOffset2) + (x * 3) + 1] = p[y * (img1.Width * 3 + nOffset) + (x * 3) + 1];       
            p2[y * (img2.Width * 3 + nOffset2) + (x * 3) + 2] = p[y * (img1.Width * 3 + nOffset) + (x * 3) +2];                                  
        }                                    
    }
}
img1.UnlockBits(bmData);
img2.UnlockBits(bmData2);
//決定檔案格式(jpeg,bmp,gif…..)

img2.Save(@"C:\pic\revise2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg );  

文章來源:https://qwweee7467.wordpress.com/2008/05/03/%E5%BD...

arrow
arrow

    Johnson峰 發表在 痞客邦 留言(0) 人氣()