C#webapiファイルストリームstreamの2つのアップロード方式「第2部base 64画像アップロード」
7367 ワード
ブログは仕事の勉強で出会った穴を記録するだけで、交流を歓迎します.
2.base 64画像アップロード
2.base 64画像アップロード
2.1クライアント
///
///
///
///
///
///
///
///
public static string SaveLearningImage(int pic_width, int pic_height, string bitMapData, string url, int saveType = 1)
{
var requstData = new
{
pic_width = pic_width,
pic_height = pic_height,
bitMapData = bitMapData,
saveType = saveType
};
return HttpPostRequst(url, JsonConvert.SerializeObject(requstData), "");
}
2.2 webapi(コード全体を添付)
[FromBody]
///
///
///
public class ImageSaveController : ApiController
{
///
///
///
//public class SaveImage
//{
private object _lock = new object();
///
///
///
///
///
///
[HttpPost]
//public string SaveLearningImage(int pic_width, int pic_height, dynamic string bitMapData, int saveType = 4)
public string SaveLearningImage([FromBody] Object bit)
{
lock (_lock)
{
//JavaScriptSerializer jss = new JavaScriptSerializer();
//string myJson = jss.Serialize(bitMapData);
//return myJson;
string jsonstr = JsonConvert.SerializeObject(bit);
Data res = JsonConvert.DeserializeObject(jsonstr);
//Data res = new Data
//{
// BitMapData = ((Newtonsoft.Json.Linq.JContainer)(bit)).First
//};
return SavePhoto(res.pic_width, res.pic_height, res.BitMapData, res.saveType);
}
}
private class Data {
public string BitMapData { get; set; }
public int pic_width { get; set; }
public int pic_height { get; set; }
public int saveType { get; set; }
}
private string SavePhoto(int pic_width, int pic_height, string bitMapData, int saveType)
{
byte[] bitmap_data = Convert.FromBase64String(bitMapData);
int PhotoD = 170;
Stream stream = new MemoryStream(bitmap_data);
var m_pic = new Bitmap(stream);
string Year = System.DateTime.Now.Year.ToString();
string Month = System.DateTime.Now.Month.ToString();
string Day = System.DateTime.Now.Day.ToString();
string Hour = System.DateTime.Now.Hour.ToString();
string ConfigPath = "", filePath = "";
switch (saveType)
{
case 4:
{
ConfigPath = ConfigurationManager.AppSettings["LearPicPath"];
filePath = "/imagepath/onlinephoto/";
break;
}
case 1:
{
ConfigPath = ConfigurationManager.AppSettings["LearPicPath_Theory"];
filePath = "/";
break;
}
}
filePath += Year + "/" + Month + "/" + Day + "/" + Hour + "/";
// ,
DirectoryInfo upDir = new DirectoryInfo(ConfigPath + filePath);
if (!upDir.Exists)
{
upDir.Create();
}
//
DateTime date = DateTime.Now;
string fileName = Guid.NewGuid().ToString() + ".jpg";
int towidth = pic_width;
int toheight = pic_height;
//towidth = PhotoD;
//toheight = PhotoD;
int width = pic_width;
int height = pic_height;
int x = 0;
int y = 0;
int ow = m_pic.Width;
int oh = m_pic.Height;
switch (ThumbnailMode.Cut)
{
case ThumbnailMode.HW:// ( )
break;
case ThumbnailMode.W:// ,
toheight = m_pic.Height * width / m_pic.Width;
break;
case ThumbnailMode.H:// ,
towidth = m_pic.Width * height / m_pic.Height;
break;
case ThumbnailMode.Cut:// ( )
if ((double)m_pic.Width / (double)m_pic.Height > (double)towidth / (double)toheight)
{
oh = m_pic.Height;
ow = m_pic.Height * towidth / toheight;
y = 0;
x = (m_pic.Width - ow) / 2;
}
else
{
ow = m_pic.Width;
oh = m_pic.Width * height / towidth;
x = 0;
y = (m_pic.Height - oh) / 2;
}
break;
default:
break;
}
// bmp
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// ,
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
g.Clear(Color.Transparent);
//
g.DrawImage(m_pic, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
string fullPath = ConfigPath + filePath + fileName;
try
{
// jpg
bitmap.Save(fullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
m_pic.Dispose();
bitmap.Dispose();
g.Dispose();
}
return filePath + fileName;
}
///
///
///
public enum ThumbnailMode
{
///
/// ( )。
///
HW,
///
/// , 。
///
W,
///
/// , 。
///
H,
///
/// ( )。
///
Cut
}
}
画像のアップロードは比較的簡単で、base 64は画像にとって、フロントエンドでもバックエンドでも便利です.
///
///
///
///
///
///
///
///
public static string SaveLearningImage(int pic_width, int pic_height, string bitMapData, string url, int saveType = 1)
{
var requstData = new
{
pic_width = pic_width,
pic_height = pic_height,
bitMapData = bitMapData,
saveType = saveType
};
return HttpPostRequst(url, JsonConvert.SerializeObject(requstData), "");
}
[FromBody]
///
///
///
public class ImageSaveController : ApiController
{
///
///
///
//public class SaveImage
//{
private object _lock = new object();
///
///
///
///
///
///
[HttpPost]
//public string SaveLearningImage(int pic_width, int pic_height, dynamic string bitMapData, int saveType = 4)
public string SaveLearningImage([FromBody] Object bit)
{
lock (_lock)
{
//JavaScriptSerializer jss = new JavaScriptSerializer();
//string myJson = jss.Serialize(bitMapData);
//return myJson;
string jsonstr = JsonConvert.SerializeObject(bit);
Data res = JsonConvert.DeserializeObject(jsonstr);
//Data res = new Data
//{
// BitMapData = ((Newtonsoft.Json.Linq.JContainer)(bit)).First
//};
return SavePhoto(res.pic_width, res.pic_height, res.BitMapData, res.saveType);
}
}
private class Data {
public string BitMapData { get; set; }
public int pic_width { get; set; }
public int pic_height { get; set; }
public int saveType { get; set; }
}
private string SavePhoto(int pic_width, int pic_height, string bitMapData, int saveType)
{
byte[] bitmap_data = Convert.FromBase64String(bitMapData);
int PhotoD = 170;
Stream stream = new MemoryStream(bitmap_data);
var m_pic = new Bitmap(stream);
string Year = System.DateTime.Now.Year.ToString();
string Month = System.DateTime.Now.Month.ToString();
string Day = System.DateTime.Now.Day.ToString();
string Hour = System.DateTime.Now.Hour.ToString();
string ConfigPath = "", filePath = "";
switch (saveType)
{
case 4:
{
ConfigPath = ConfigurationManager.AppSettings["LearPicPath"];
filePath = "/imagepath/onlinephoto/";
break;
}
case 1:
{
ConfigPath = ConfigurationManager.AppSettings["LearPicPath_Theory"];
filePath = "/";
break;
}
}
filePath += Year + "/" + Month + "/" + Day + "/" + Hour + "/";
// ,
DirectoryInfo upDir = new DirectoryInfo(ConfigPath + filePath);
if (!upDir.Exists)
{
upDir.Create();
}
//
DateTime date = DateTime.Now;
string fileName = Guid.NewGuid().ToString() + ".jpg";
int towidth = pic_width;
int toheight = pic_height;
//towidth = PhotoD;
//toheight = PhotoD;
int width = pic_width;
int height = pic_height;
int x = 0;
int y = 0;
int ow = m_pic.Width;
int oh = m_pic.Height;
switch (ThumbnailMode.Cut)
{
case ThumbnailMode.HW:// ( )
break;
case ThumbnailMode.W:// ,
toheight = m_pic.Height * width / m_pic.Width;
break;
case ThumbnailMode.H:// ,
towidth = m_pic.Width * height / m_pic.Height;
break;
case ThumbnailMode.Cut:// ( )
if ((double)m_pic.Width / (double)m_pic.Height > (double)towidth / (double)toheight)
{
oh = m_pic.Height;
ow = m_pic.Height * towidth / toheight;
y = 0;
x = (m_pic.Width - ow) / 2;
}
else
{
ow = m_pic.Width;
oh = m_pic.Width * height / towidth;
x = 0;
y = (m_pic.Height - oh) / 2;
}
break;
default:
break;
}
// bmp
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// ,
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
g.Clear(Color.Transparent);
//
g.DrawImage(m_pic, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
string fullPath = ConfigPath + filePath + fileName;
try
{
// jpg
bitmap.Save(fullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
m_pic.Dispose();
bitmap.Dispose();
g.Dispose();
}
return filePath + fileName;
}
///
///
///
public enum ThumbnailMode
{
///
/// ( )。
///
HW,
///
/// , 。
///
W,
///
/// , 。
///
H,
///
/// ( )。
///
Cut
}
}