locked
how to rotate and upload an image RRS feed

  • Question

  • User59632901 posted

    I want to rotate an image and upload it from some path .But I dont want to use Session to keep path.I searched a lot but none of them good for me .

    Thanks for any advance.

    Friday, September 28, 2018 11:09 AM

All replies

  • User753101303 posted

    Hi,

    Unclear.

    You want to rotate the image before the upload ? It would be likely simpler to upload the image using https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.fileupload?view=netframework-4.7.2 and then do the rotation using https://docs.microsoft.com/en-us/dotnet/api/system.drawing.image.rotateflip?view=netframework-4.7.2 (this is a 90° multiple).

    Or you want to grab the file data rotate on the client side and then post the resulting file content. It is likely significantly harder.

    In both case I really don't see why you would have a to keep a path in Session ???

    If you thought it is possible the only way to upload a file is using the control above (it may seem to work on a dev machine but keep in mind that a real web server won't have access to a client side disk).

    Friday, September 28, 2018 12:17 PM
  • User59632901 posted

    Let me clear it for you.

    Firt of all I want to upload an image from Fileupload, but when the image has been loaded and when I click the Rotate button, the RotateButton_Click event fired ,and the FileUpload goes to empty.Because of that I keep the FileUpload data in the Session["FileUpload"] and from there I access the previous data .But in that case  for example 'I uploaded an image path to database ' if ı want to upload some other image I think because of the Session my image didnt upload correctly.Okey let me share with you some codes;

    static int count = 1;
    static int degisken = 0; // Dosyanın işlem geçirip geçirmediği kontrolleri
    static string oncekiPath = "";
    static string durum;
    Random rnd;
    Bitmap _current;
    /*
    * Ana görseli döndürme, önceki resimin silinme işlemi bu eventte yapılır
    * ayrıca gerekli son resim url sine uzantı atımı da burda yapılır.
    */
    protected void btnRotate_Click(object sender, EventArgs e)  //Click Event
    {
    if (fupanagorsel.HasFile && durum == null)
    {
    string filePath = Server.MapPath("/image/" + fupanagorsel.FileName);
    fupanagorsel.SaveAs(filePath);
    urunresmi.ImageUrl = "/image/" + fupanagorsel.FileName;
    Session["filepath"] = "/image/" + fupanagorsel.FileName;
    durum = "Basarili!";
    }
    //Yükleme yapıldıysa if e girer..
    if (durum == "Basarili!")
    {
    var gorselYolu = Session["filepath"].ToString();
    Bitmap temp = (Bitmap)Bitmap.FromFile(Server.MapPath(gorselYolu));
    Bitmap bmap = (Bitmap)temp.Clone();

    switch (count)
    {
    case 1:
    bmap.RotateFlip(RotateFlipType.Rotate270FlipXY);
    count++;
    degisken = 1;
    break;
    case 2:
    bmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
    count++;
    System.IO.File.Delete(oncekiPath);
    break;
    case 3:
    bmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
    count++;
    System.IO.File.Delete(oncekiPath);
    break;
    case 4:
    bmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);
    count = 1;
    System.IO.File.Delete(oncekiPath);
    break;
    default:
    break;
    }
    _current = (Bitmap)bmap.Clone();//Clone oluştur mevcut resmin
    rnd = new Random(); //Random sayı

    int random = rnd.Next();
    _current.Save(Server.MapPath("/image/" + random + ".png"));
    sonresimUrl = random + ".png";
    urunresmi.ImageUrl = "/image/" + random + ".png";
    //db deki url si ni kaydeder.
    oncekiPath = Server.MapPath(urunresmi.ImageUrl); //Nerden silicemizin pathi(bir önceki rotate image inin pathi)....
    }

    }

    Friday, September 28, 2018 12:41 PM
  • User-1952463932 posted

    I think the reason that you are having an issue is because you are uploading during one callback and rotating during another.  That is during the first call back you do the upload, and then the user clicks rotate which results in a second call back.  FileUpload only holds its information for one call back, after that the information that the control has is lost, this is done for security reasons.  What I suggest is that you do your upload and rotate in one call back that way you don't have to save your data in Session or anywhere else.

    An alternative is upon file upload to save the file to server but then save the file path and name to the ViewState and then refer to the file upon the second call back to rotate.

    Your current code is some flaws in it that will cause it to not work correctly under certain circumstances.  If you post your aspx code that would help to see what user flow you are trying to accomplish. 

    Sunday, September 30, 2018 8:22 PM
  • User-271186128 posted

    Hi afozbek,

    But in that case  for example 'I uploaded an image path to database ' if ı want to upload some other image I think because of the Session my image didnt upload correctly.

    if (fupanagorsel.HasFile && durum == null)
    {
    string filePath = Server.MapPath("/image/" + fupanagorsel.FileName);
    fupanagorsel.SaveAs(filePath);
    urunresmi.ImageUrl = "/image/" + fupanagorsel.FileName;
    Session["filepath"] = "/image/" + fupanagorsel.FileName;
    durum = "Basarili!";
    }

    Based on your description and above code,  when upload another image, if the durum isn't empty, it will not set the value of the session. So, I suggest you check your logic and the if condition. You could add some break point to debug your code in debug mode.

    Best regards,
    Dillion

    Monday, October 1, 2018 3:20 AM
  • User59632901 posted

    I do that too,I write the logic inside the Rotate button event And get the image with Jquery.But I have 2 FileUploads and I am using this code to save the data inside the FileUpload

    private void controlAnaFile()
    {
    if (fupanagorsel.HasFile)
    {
    Session["FileUpload1"] = fupanagorsel;
    // Avoid the filestream closed
    Session["fileStream"] = new MemoryStream(fupanagorsel.FileBytes);
    }
    // Now there could be another sictution when Session has File but user want to change the file
    // In this case we have to change the file in session object
    else if (Session["FileUpload1"] != null && (!fupanagorsel.HasFile))
    {
    fupanagorsel = (FileUpload)Session["FileUpload1"];
    }
    string FileName = fupanagorsel.FileName;
    // Avoid file stream to be closed.
    var FileStream = (Stream)Session["fileStream"];
    }

    The above code runs each PostBack time (in PageLoad)

    Monday, October 1, 2018 6:44 AM
  • User59632901 posted

    thank you for your answer , I see that is somethings wrong about my logic .

    Monday, October 1, 2018 6:45 AM