private void button5_Click(object sender, EventArgs e)
{
string strtext = "https://s1.yimg.com/bt/api/res/1.2/tg4y9A._NGcShMlg5HOcUQ--/YXBwaWQ9eW5ld3NfbGVnbztmaT1pbnNldDtoPTM3MjtpbD1wbGFuZTtxPTc5O3c9NjYy/http://media.zenfs.com/zh_hant_tw/News/tvbs/TVBS-N_CLEAN_10M_20151110_13-40-03.mp4_20151110_141351.278.jpg";
WebRequest request = WebRequest.Create(new Uri(strtext));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.ContentLength > 0)
{
pictureBox1.Image = BufferToImage(CompressionImage(response.GetResponseStream(), 100));
}
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
private byte[] CompressionImage(Stream fileStream, long quality)
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream))
{
int h, w;
if (img.Width <= 100)
{
h = img.Height;
w = img.Width;
}
else
{
w = 100;
h = 100 * img.Height / img.Width;
}
using (Bitmap bitmap = new Bitmap(img, w, h))//預設為長寬各為100px
{
ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
myEncoderParameters.Param[0] = myEncoderParameter;
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, CodecInfo, myEncoderParameters);
myEncoderParameters.Dispose();
myEncoderParameter.Dispose();
return ms.ToArray();
}
}
}
}
/// <summary>
/// 將 Byte 陣列轉換為 Image。
/// </summary>
/// <param name="Buffer">Byte 陣列。</param>
private static System.Drawing.Image BufferToImage(byte[] Buffer)
{
if (Buffer == null || Buffer.Length == 0) { return null; }
byte[] data = null;
System.Drawing.Image oImage = null;
Bitmap oBitmap = null;
//建立副本
data = (byte[])Buffer.Clone();
try
{
MemoryStream oMemoryStream = new MemoryStream(Buffer);
//設定資料流位置
oMemoryStream.Position = 0;
oImage = System.Drawing.Image.FromStream(oMemoryStream);
//建立副本
oBitmap = new Bitmap(oImage);
}
catch
{
throw;
}
//return oImage;
return oBitmap;
}
{
string strtext = "https://s1.yimg.com/bt/api/res/1.2/tg4y9A._NGcShMlg5HOcUQ--/YXBwaWQ9eW5ld3NfbGVnbztmaT1pbnNldDtoPTM3MjtpbD1wbGFuZTtxPTc5O3c9NjYy/http://media.zenfs.com/zh_hant_tw/News/tvbs/TVBS-N_CLEAN_10M_20151110_13-40-03.mp4_20151110_141351.278.jpg";
WebRequest request = WebRequest.Create(new Uri(strtext));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.ContentLength > 0)
{
pictureBox1.Image = BufferToImage(CompressionImage(response.GetResponseStream(), 100));
}
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
private byte[] CompressionImage(Stream fileStream, long quality)
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream))
{
int h, w;
if (img.Width <= 100)
{
h = img.Height;
w = img.Width;
}
else
{
w = 100;
h = 100 * img.Height / img.Width;
}
using (Bitmap bitmap = new Bitmap(img, w, h))//預設為長寬各為100px
{
ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
myEncoderParameters.Param[0] = myEncoderParameter;
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, CodecInfo, myEncoderParameters);
myEncoderParameters.Dispose();
myEncoderParameter.Dispose();
return ms.ToArray();
}
}
}
}
/// <summary>
/// 將 Byte 陣列轉換為 Image。
/// </summary>
/// <param name="Buffer">Byte 陣列。</param>
private static System.Drawing.Image BufferToImage(byte[] Buffer)
{
if (Buffer == null || Buffer.Length == 0) { return null; }
byte[] data = null;
System.Drawing.Image oImage = null;
Bitmap oBitmap = null;
//建立副本
data = (byte[])Buffer.Clone();
try
{
MemoryStream oMemoryStream = new MemoryStream(Buffer);
//設定資料流位置
oMemoryStream.Position = 0;
oImage = System.Drawing.Image.FromStream(oMemoryStream);
//建立副本
oBitmap = new Bitmap(oImage);
}
catch
{
throw;
}
//return oImage;
return oBitmap;
}
沒有留言:
張貼留言