简体   繁体   English

在c#中上传时在图像上添加日期

[英]Add date on image while uploading in c#

I am adding date and image to the database. 我正在向数据库添加日期和图像。 I want to add the date as the footer to the uploaded image. 我想将日期作为页脚添加到上传的图像。

HTML for image upload 用于图片upload HTML

<div class="form-group">
    @Html.Label("Photo", htmlAttributes: new { @class = "control-label" })
    <div class="col-md-10">
        <img id="DocImg" src="~/Upload/DoctorImage/doc-default.png" style="cursor: pointer;"  accesskeyclass="edtImg" width="100" height="100" />
        <input type="file" id="fileUpload" name="Photo" accept="image/*" />
    </div>
</div>

HTML for datepicker datepicker HTML

 <div class="col-6">
    @Html.ValidationSummary(true, "", new { @class = "text-danger"})
    <div class="form-group">
        @Html.Label("Joining Date", htmlAttributes: new { @class = "control-label col-md-2", @required = "required" })
        <div class="col-md-10">
            @(Html.Kendo().DatePicker()
                            .Name("JoiningDate")
                            .Value("")
                            .HtmlAttributes(new { style = "width: 100%", required = "true" })
            )
            @Html.ValidationMessageFor(model => model.JoiningDate, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

Script for Upload 上传脚本

$(document).ready(function () {
    $("#fileUpload").hide();
});

$("#DocImg").click(function () {
    $("#fileUpload").trigger('click');
});

I think you need to manipulate the image in the DOM before the post completes, using ajax to accomplish it 我认为你需要在帖子完成之前操纵DOM中的图像,使用ajax来完成它

Having this in mind, all you need is to use a canvas to render the text from the datepicker inside the image. 考虑到这一点,您只需使用画布来渲染图像中的日期选择器中的文本。

As shown in the code - 如代码所示 -

 var canvasEl = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); canvasEl.width = $('img').width(); canvasEl.crossOrigin = "Anonymous"; canvasEl.height = $('img').height(); ctx.drawImage($('img').get(0), 0, 0); ctx.font = "36pt Verdana"; $(document).on('input','#inp',function(){ //redraw image ctx.clearRect(0,0,canvasEl.width,canvasEl.height); ctx.drawImage($('img').get(0), 0, 0); //refill text ctx.fillStyle = "red"; ctx.fillText($(this).val(),40,80); //positioning text wherever you want }); $('button').click(function(){ console.log(ctx.getImageData(50, 50, 100, 100)); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <img style="display:none" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTsEbEB4kEMHt3WJ13HpZE16hJ3iKrE8ugnErvyln7oNPDma48U" crossorigin="anonymous"/> <input type="text" id="inp"/> <button type="submit">Save</button> </form> <canvas id="canvas" /> 

Please share if it works for you? 请分享它是否适合您?

You gonna need a reference to System.Drawing and play a little bit with graphics. 你需要一个System.Drawing的引用,并玩一点图形。 I just authored a solution for you so it isn't bulletproof. 我刚刚为你写了一个解决方案,所以它不是防弹的。 You might wanna play around to change the colors, font, etc. as well as add exception handling. 您可能想要改变颜色,字体等,以及添加异常处理。

public void AddWatermark(string inputhPath, string outputPath)
{
    var wm = DateTime.Now.ToShortDateString();

    using (var bmp = Bitmap.FromFile(inputhPath))
    using (var g = Graphics.FromImage(bmp))
    using (var transparentBrush = new SolidBrush(Color.FromArgb(164, 0, 0, 0)))
    using (var font = new Font("Calibri", 20, FontStyle.Bold, GraphicsUnit.Pixel))
    {
        var format = new StringFormat(StringFormatFlags.NoWrap);
        var dim = g.MeasureString(wm, font);
        var loc = new Point(bmp.Width - (int)dim.Width - 20, bmp.Height - (int)dim.Height * 2);

        g.DrawString(wm, font, transparentBrush, loc);

        bmp.Save(outputPath);
    }
}

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

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