繁体   English   中英

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

[英]Save an Image in database with written text in C#

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

您能同时在db图片中保存文本吗?

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);
}

这是没有文字的普通图片

添加文字后

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

谢谢。

您粘贴的代码仅与在位图对象上添加文本并将位图对象保存回添加了文本的物理文件有关。 没有代码将此图像保存在数据库中。

//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;
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM