使用C#中的书面文本将图像保存在数据库中

时间:2017-02-22 04:53:53

标签: c# asp.net-mvc

它显示Image上的文本但不保存在数据库中。没有文本的上一张图像保存在db。

您可以帮助在db。中的Image上保存文本。

public void ImageText()
{
            Image bitmap = Image.FromFile(Server.MapPath("~/Image/IMG_20160930_082316.jpg"));
            //draw the image object using a Graphics object
            Graphics graphicsImage = Graphics.FromImage(bitmap);
            //Set the alignment based on the coordinates   
            StringFormat stringformat = new StringFormat();
            stringformat.Alignment = StringAlignment.Far;
            stringformat.LineAlignment = StringAlignment.Far;
            StringFormat stringformat2 = new StringFormat();
            stringformat2.Alignment = StringAlignment.Center;
            stringformat2.LineAlignment = StringAlignment.Center;
            //Set the font color/format/size etc..  
            Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding
            Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding
            string Str_TextOnImage = "Hello";//Your Text On Image
            string Str_TextOnImage2 = "Word";//Your Text On Image
            graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40,
            FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245),
            stringformat); Response.ContentType = "image/jpeg";
            graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111,
            FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255),
            stringformat2); Response.ContentType = "image/jpeg";
            bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
}

This is an Normal Image without text

After Add Text

但无法将文本图像保存到DB中。请帮帮我。

感谢。

2 个答案:

答案 0 :(得分:0)

您粘贴的代码仅涉及在位图对象上添加文本并将位图对象保存回添加文本的物理文件。没有代码可以在DB中保存此图像。

答案 1 :(得分:0)

//Post method for Image upload
     [HttpPost]
            public async Task<JsonResult> AddPromotion(AddPromotionDto addpromotions)
            {
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase files = Request.Files;
                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFileBase file = files[i];
                        string promotionImage;
                        addpromotions.PromotionImage = file.FileName;
                        promotionImage = Path.Combine(Server.MapPath("../Image/"), addpromotions.PromotionImage);
                        file.SaveAs(promotionImage);
                        using (var fileStream = new MemoryStream())
                        {
                            using (var oldImage = new Bitmap(file.InputStream))
                            {
                                var format = oldImage.RawFormat;
                                string fileName = promotionImage;
                                using (var newImage = ResizeImage(oldImage, 800, 2000))
                                {
                                    Graphics graphicsImage = Graphics.FromImage(newImage);
                                    StringFormat stringformat = new StringFormat();
                                    stringformat.Alignment = StringAlignment.Far;
                                    stringformat.LineAlignment = StringAlignment.Far;
                                    StringFormat stringformat2 = new StringFormat();
                                    stringformat2.Alignment = StringAlignment.Center;
                                    stringformat2.LineAlignment = StringAlignment.Center;
                                    Color StringColor = System.Drawing.ColorTranslator.FromHtml("#e80c88");
                                    string Str_TextOnImage = addpromotions.PromotionName;
                                    graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40,
                                    FontStyle.Regular), new SolidBrush(StringColor), new Point(500, 300),
                                    stringformat);
                                    Response.ContentType = "image/jpeg";
                                    newImage.Save(fileName, format); // for overwrite the previous image
                                }
                            }
                        }
                    }
                }
                await companyManager.AddPromotion(User.Identity.Name, addpromotions);
                return Json("Promotions", JsonRequestBehavior.AllowGet);
            }


    // Method for resize the image
     public static Bitmap ResizeImage(Bitmap image, int width, int height)
            {
                if (image.Width <= width && image.Height <= height)
                {
                    return image;
                }
                int newWidth;
                int newHeight;
                if (image.Width > image.Height)
                {
                    newWidth = width;
                    newHeight = (int)(image.Height * ((float)width / image.Width));
                }
                else
                {
                    newHeight = height;
                    newWidth = (int)(image.Width * ((float)height / image.Height));
                }
                var newImage = new Bitmap(newWidth, newHeight);
                using (var graphics = Graphics.FromImage(newImage))
                {
                    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    graphics.FillRectangle(Brushes.Transparent, 0, 0, newWidth, newHeight);
                    graphics.DrawImage(image, 0, 0, newWidth, newHeight);
                    return newImage;
                }
            }