二进制流操作
/// <summary>
/// 給二進制流添加水印文字并返回
/// </summary>
/// <param name="photo">二進制流</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="watermarkStatus">圖片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
/// <param name="quality">附加水印圖片質量,0-100</param>
/// <param name="fontsize">字體大小</param>
/// <param name="fontname">字體</param>
/// <returns></returns>
public byte[] AddWaterText(Byte[] photo, string watermarkText,
int watermarkStatus, int quality, int fontsize, string fontname)
{
Image img = Image.FromStream(new System.IO.MemoryStream(photo));//將圖片流轉換為縮略圖并固定尺寸
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image imgs=img.GetThumbnailImage(300, 300, callb, IntPtr.Zero);Graphics grap = Graphics.FromImage(imgs);
Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF crSize;
crSize = grap.MeasureString(watermarkText, drawFont);
float xpos = 0;
float ypos = 0;
switch (watermarkStatus)
{
case 1:
xpos = (float)imgs.Width * (float).01;
ypos = (float)imgs.Height * (float).01;
break;
case 2:
xpos = ((float)imgs.Width * (float).50) - (crSize.Width / 2);
ypos = (float)imgs.Height * (float).01;
break;
case 3:
xpos = ((float)imgs.Width * (float).99) - crSize.Width;
ypos = (float)imgs.Height * (float).01;
break;
case 4:
xpos = (float)imgs.Width * (float).01;
ypos = ((float)imgs.Height * (float).50) - (crSize.Height / 2);
break;
case 5:
xpos = ((float)imgs.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)imgs.Height * (float).50) - (crSize.Height / 2);
break;
case 6:
xpos = ((float)imgs.Width * (float).99) - crSize.Width;
ypos = ((float)imgs.Height * (float).50) - (crSize.Height / 2);
break;
case 7:
xpos = (float)imgs.Width * (float).01;
ypos = ((float)imgs.Height * (float).99) - crSize.Height;
break;
case 8:
xpos = ((float)imgs.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)imgs.Height * (float).99) - crSize.Height;
break;
case 9:
xpos = ((float)imgs.Width * (float).99) - crSize.Width;
ypos = ((float)imgs.Height * (float).99) - crSize.Height;
break;
}grap.DrawString(watermarkText, drawFont, new SolidBrush(Color.Blue), xpos, ypos);byte[] bt = null;
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(imgs);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Png);//將圖像以指定的格式存入緩存內存流
bt = new byte[mostream.Length];
mostream.Position = 0;//設置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
return bt;
}調用方式://給二進制流加水印文字 byte[] imgData = AddWaterText(item.SignetName, "僅限預覽", 5, 50, 15, "宋體");-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------/// <summary>
/// 將二進制流轉換為base64
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public string ByteToByte64String(byte[] data)
{
string img = Convert.ToBase64String(data);
return img;
}調用方式://二進制流轉換為base64byte[] imgData=new byte[0];string imgBase64=this.ByteToByte64String(imgData);前臺界面顯示let img="data:image/png;base64,"+imgBase64<img src=img/>--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------/// <summary>
/// 圖片轉換二進制流方法
/// </summary>
/// <param name="path">文件路徑</param>
/// <returns></returns>
private byte[] Imgbytefromimg(string path)
{//方式一
//FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
//Byte[] imgByte = new Byte[f.Length];//把圖片轉成 Byte型 二進制流
//f.Read(imgByte, 0, imgByte.Length);//把二進制流讀入緩沖區
//f.Close();
//return imgByte;//方式二byte[] bytes = System.IO.File.ReadAllBytes(path); //讀取文件到byte數組
return bytes;
}
轉載于:https://www.cnblogs.com/YYkun/p/10148821.html
總結
- 上一篇: XAF-BI.Dashboard模块概述
- 下一篇: Spring----JmsTemplat