locked
Saving graphics in image and also to show it in the form (not able to see my graphics on my page if saving in file)) RRS feed

  • Question

  • User505287191 posted

    Hi Big,

    I am trying to save a graphics as a image in my folder at runtime.

    I am able to create my graphics. I am trying to save it in a file. But when I am doing so I can't see it in my form. It is saving in file but cannot see see it in my form.

    I am using this line to do so

    Image bmp = new Bitmap(100, 100);
                Graphics g1 = Graphics.FromImage(bmp);

    If I am not using the line below then just using graphics object I am able to create my graphic and able to show in my form. But as I use this line I cannot see my graphic on my form.

    Graphics g1 = Graphics.FromImage(bmp);

    My requirement is first I need to show image on the form and when I press a button It should be saved in a file. Is this possible that I can copy whole graphics from my object g into the object g1 and through g1 I should save the image.

    Please help.

    Monday, January 16, 2012 5:01 AM

Answers

All replies

  • User1829879277 posted

    You can save the image in the DB in a binary format.

    OR

    Save the image file on the server at particular location and save this file location in to database.

    Monday, January 16, 2012 5:12 AM
  • User505287191 posted

    Hi Big,

    I need to save it in my file. Please go through the question one more time. Perhaps you may help me.

    Monday, January 16, 2012 5:26 AM
  • User1829879277 posted

    Try this:

    Image img = Image.FromFile(Server.MapPath("a.png")); 
    int w = img.Width; 
    int h = img.Height; 
     
    //Create an empty bitmap with scaled size,here half 
    Bitmap bmp = new Bitmap(w / 2, h / 2); 
    //Create graphics object to draw 
    Graphics g = Graphics.FromImage(bmp); 
    //You can also use SmoothingMode,CompositingMode and CompositingQuality 
    //of Graphics object to preserve saving options for new image.         
     
    //Create drawing area with a rectangle 
    Rectangle drect = new Rectangle(0, 0, bmp.Width, bmp.Height); 
    //Draw image into your rectangle area 
    g.DrawImage(img, drect); 
    //Save your new image 
    bmp.Save(Server.MapPath("a2.jpg"), ImageFormat.Jpeg); 
    
    
    Monday, January 16, 2012 5:50 AM
  • User505287191 posted

    Hi BIg,

    I already have tried this. Actually I can do one of them. Either I can create a graphic to show in form or can do this to save in file. But I want both.

    Is there any posibility that I can first create my graphic in one object and can copy it to another object to save.

    See I explain it through an example.

    Let say

    Graphics g;

    g=panel1.CreateGraphics;

    g.DrawRectangle(Pens.Black, new Rectangle(10,10,100,100));

    It will create a graphic on panel.

    But if i want to save a graphic then I need to write something like.

    Bitmap bmp = new Bitmap(100,100); 
    Graphics g1 = Graphics.FromImage(bmp); 
    g1.DrawRectangle(Pens.Black, new Rectangle(10,10,100,100));

    bmp.Save(Server.MapPath("a2.jpg"), ImageFormat.Jpeg); 

    Now what I want that object g should be copy in g1 so that I can save it also.
    Now have you some solution.

    Please reply its urgent.


    Monday, January 16, 2012 6:44 AM
  • User1829879277 posted

    On the net it has been mentioned that we can't copy a Graphics object to another. But can create new using existing.

    Refer these links:

    http://msdn.microsoft.com/en-us/library/aa457087.aspx

    http://stackoverflow.com/questions/597037/how-to-copy-one-graphics-object-into-another

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 16, 2012 7:08 AM
  • User2120079109 posted

    You can save the image in the DB in a binary format.

    Friday, March 16, 2012 7:14 PM