ASP.NET
6
Вклад в тег
private readonly IApplicationEnvironment _appEnvironment;
public YourController(IApplicationEnvironment appEnvironment)
{
_appEnvironment = appEnvironment;
}
public ActionResult Create()
{
YourModel model = new YourModel();
string path = _appEnvironment.ApplicationBasePath + "\\wwwroot\\Content\\Default"; // получим путь к файловой системе
model.Photo = ImageToByte(path + "\\Image.png");
}
private byte[] ImageToByte(string path)
{
Image image = Image.FromFile(path);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
return memoryStream.ToArray();
}
@Html.Raw("<img style='width:50px;' src=\"data:image/jpeg;base64," + Convert.ToBase64String(Model.Photo) + "\" />")
public async Task<ActionResult> Create( Shop model, HttpPostedFileBase PhotoImage)
{
if (ModelState.IsValid)
{
byte[] imageData = null;
using (var binaryReader = new BinaryReader(PhotoImage.InputStream))
{
imageData = binaryReader.ReadBytes(PhotoImage.ContentLength);
}
model.Photo = imageData;
db.Shops.Add(model);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(model);
}
<input type="file" name="PhotoImage" />
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
Document doc = new Document(PageSize.A4, 30f, 30f, 30f, 30f);
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
BaseFont baseFont = BaseFont.CreateFont("C:\\Windows\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//Заголовок
PdfContentByte cb = writer.DirectContent;
cb.Rectangle(10f, 10f, doc.PageSize.Width - 20f, doc.PageSize.Height - 20f);
cb.SetFontAndSize(baseFont, 13);
cb.BeginText();
cb.ShowTextAligned(
PdfContentByte.ALIGN_CENTER, "Отчёт", 300f, doc.PageSize.Height - 30f, 0);
cb.EndText();
cb.Stroke();
doc.Close();
}