using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Web.Mvc; using System.Drawing; using System.IO; using System.IO.Compression; using System.Data.Linq; using System.Linq.Expressions; using Elisy.LinqTo1CSql.УправлениеТорговлейДляКазахстана; namespace Controllers { [HandleError] public class LinqImages : ApplicationController { [OutputCache(Duration = 60, VaryByParam = "code;width;height")] public ImageActionResult Фото(string code, int width, int height) { int resulution = 96; Bitmap bitmap = null; var изображение = from дополнительнаяИнформация in DataContext.GetTable<СправочникХранилищеДополнительнойИнформации>() join номенклатура in DataContext.GetTable<СправочникНоменклатура>() on дополнительнаяИнформация.Ссылка equals номенклатура.ОсновноеИзображение where номенклатура.Код == code select new {ИмяФайла = дополнительнаяИнформация.ИмяФайла, Хранилище = дополнительнаяИнформация.Хранилище}; if (изображение.Count() == 0) { bitmap = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.LightGray); SolidBrush brush = new SolidBrush(Color.Silver); Font font = new Font("Arial", 18, FontStyle.Bold | FontStyle.Italic); graphics.DrawString("Без фото", font, brush, 10, 10); graphics.Flush(); font.Dispose(); brush.Dispose(); graphics.Dispose(); } else { //Deflated data byte[] valueStorageData = изображение.First().Хранилище.ToArray(); if (valueStorageData[0] == 2) //Deflated not supported ValueStorage { bitmap = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.LightGray); SolidBrush brush = new SolidBrush(Color.Silver); Font font = new Font("Arial", 18, FontStyle.Bold | FontStyle.Italic); graphics.DrawString("Без фото", font, brush, 10, 10); graphics.DrawString("Неверный", font, brush, 10, 30); graphics.DrawString("формат", font, brush, 10, 50); graphics.DrawString("сжатия", font, brush, 10, 70); graphics.Flush(); font.Dispose(); brush.Dispose(); graphics.Dispose(); } else if (valueStorageData[0] == 1) { //Type Картинка: e6f51714-91cb-4dce-94fe-90ae3e3e1ad1 MemoryStream valueStorageMemoryStream = new MemoryStream(valueStorageData); //Data size then data valueStorageMemoryStream.Position = 0x64; BinaryReader binaryReader = new BinaryReader(valueStorageMemoryStream); UInt64 dataSize = binaryReader.ReadUInt64(); byte[] imageData = new byte[dataSize]; valueStorageMemoryStream.Read(imageData, 0, (int)dataSize); MemoryStream imageMemoryStream = new MemoryStream(imageData); bitmap = new Bitmap(imageMemoryStream); } } return new ImageActionResult() { Bitmap = bitmap, Width = width, Height = height, Resolution = resulution }; } } public class ImageActionResult : ActionResult { public Bitmap Bitmap { get; set; } public int Width { get; set; } public int Height { get; set; } public int Resolution { get; set; } public override void ExecuteResult(ControllerContext context) { Bitmap outputBitmap = new Bitmap(Width, Height); Graphics g = Graphics.FromImage(outputBitmap); g.Clear(Color.WhiteSmoke); float sx = (float)Width / Bitmap.Width; float sy = (float)Height / Bitmap.Height; float scale = Math.Min(sx, sy); g.DrawImage(Bitmap, (outputBitmap.Width - scale * Bitmap.Width) / 2, (outputBitmap.Height - scale * Bitmap.Height) / 2, scale * Bitmap.Width, scale * Bitmap.Height); context.HttpContext.Response.ContentType = "image/jpeg"; outputBitmap.Save(context.HttpContext.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } } }