[SOLVED] How to use the upload and screenshot controls?


I have an asp.net core mvc project, and I need a function request, the function of uploading pictures and allowing users to take screenshots at will, I goole a lot, I currently find a screenshot of a form, is there any upload function in the view?

private void button3_Click(object sender, EventArgs e)
            Bitmap myImage = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(myImage);
            g.CopyFromScreen(new Point(this.Location.X, this.Location.Y), new Point(0, 0), new Size(this.Width, this.Height));
            IntPtr t1= g.GetHdc();


If you just want to upload pictures and then take screenshots, and then upload the screenshots to the backend, you can refer to this demo, which may be helpful to you.

First create an Images folder under your wwwroot.

Controller code:

  public class ImageController : Controller
        private IHostingEnvironment Environment;

        public ImageController(IHostingEnvironment _environment)
            Environment = _environment;

        public IActionResult Index()
            return View();

        public IActionResult Save()
            string base64 = Request.Form["imgCropped"];
            byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]);

            string filePath = Path.Combine(this.Environment.WebRootPath, "Images", "Test.png");
            using (FileStream stream = new FileStream(filePath, FileMode.Create))
                stream.Write(bytes, 0, bytes.Length);
            return RedirectToAction("Index");

View code:

@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
    Layout = null;

<!DOCTYPE html>

    <meta name="viewport" content="width=device-width" />
    <form method="post" enctype="multipart/form-data" asp-controller="Image" asp-action="Save">
        <input type="file" id="FileUpload1" />
        <br />
        <br />
        <table border="0" cellpadding="0" cellspacing="5">
                    <img id="Image1" src="" alt="" style="display: none" />
                    <canvas id="canvas" height="5" width="5"></canvas>
        <br />
        <input type="button" id="btnCrop" value="Crop" style="display: none" />
        <input type="submit" id="btnUpload" value="Upload" style="display: none" />
        <input type="hidden" name="imgX1" id="imgX1" />
        <input type="hidden" name="imgY1" id="imgY1" />
        <input type="hidden" name="imgWidth" id="imgWidth" />
        <input type="hidden" name="imgHeight" id="imgHeight" />
        <input type="hidden" name="imgCropped" id="imgCropped" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.9/js/jquery.Jcrop.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#FileUpload1').change(function () {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('#Image1').attr("src", e.target.result);
                        onChange: SetCoordinates,
                        onSelect: SetCoordinates

            $('#btnCrop').click(function () {
                var x1 = $('#imgX1').val();
                var y1 = $('#imgY1').val();
                var width = $('#imgWidth').val();
                var height = $('#imgHeight').val();
                var canvas = $("#canvas")[0];
                var context = canvas.getContext('2d');
                var img = new Image();
                img.onload = function () {
                    canvas.height = height;
                    canvas.width = width;
                    context.drawImage(img, x1, y1, width, height, 0, 0, width, height);
                img.src = $('#Image1').attr("src");
        function SetCoordinates(c) {


enter image description here

You can also use the Croppie plugin, read this documentation:


Answered By – Chaodeng

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *