locked
how can i create image at runtime. RRS feed

  • Question

  • User2057230362 posted

    i want to be able to adjust the width/height and the src property of the image control.

    Tuesday, December 16, 2008 3:21 PM

Answers

  • User-1760427068 posted

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB_LoadResizePicture.aspx.vb" Inherits="VB_LoadResizePicture" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
    
        <asp:FileUpload id="MyFileUploadControl" Width="400" runat="server" />        
        <asp:Button runat="server" id="AddAttachmentButton" text="Upload" /><br />
        
        Thumbnail: <asp:Image ID="MyThumbnail" runat="server" /><br /><br />
    
        </form>
    </body>
    </html>

      

    Imports System.IO 'needed to enable .Delete
    Imports System.IO.Path
    Imports System.Drawing  'needed to enable Bitmap
    Imports System.Drawing.Imaging
    
    Partial Class VB_LoadResizePicture
        Inherits System.Web.UI.Page
    
    
    
        Protected Sub AddAttachmentButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddAttachmentButton.Click
    
            If MyFileUploadControl.HasFile Then
    
                Dim newfile As String = MyFileUploadControl.FileName
                MyFileUploadControl.SaveAs(Server.MapPath("~/somefolder/original") + newfile)
    
                Dim bmp1 As New Bitmap(Server.MapPath("~/somefolder/original") + newfile)
    
                Dim bmpheight As Double = Convert.ToDouble(bmp1.Height)
                Dim bmpwidth As Double = Convert.ToDouble(bmp1.Width)
    
                Dim newbmpheight As Integer = Convert.ToInt32(bmpheight / 4)
                Dim newbmpwidth As Integer = Convert.ToInt32(bmpwidth / 4)
    
                Dim bmp2 As Bitmap = bmp1.GetThumbnailImage(newbmpwidth, newbmpheight, Nothing, New IntPtr)
    
                bmp2.Save(Server.MapPath("~/somefolder/") + MyFileUploadControl.FileName, ImageFormat.Jpeg)
    
                MyThumbnail.ImageUrl = "somefolder/" + newfile
    
                bmp1.Dispose()
    
                Dim TheFile As FileInfo = New FileInfo(Server.MapPath("~/somefolder/original") + newfile)
                If TheFile.Exists Then
    
                    File.Delete(Server.MapPath("~/somefolder/original") + newfile)
    
                End If
    
            End If
    
        End Sub
    
    End Class
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 17, 2008 2:15 PM

All replies

  • User-1760427068 posted

    Can you elaborate with an example?

    For example, do you want a program that will upload a file from a client machine (maybe a 400px x 400px square) and change it to 100px x 100px before saving it into a folder on a server?  Am I going in the right direction?

    -Larry

    Tuesday, December 16, 2008 3:58 PM
  • User2057230362 posted

    yes.... but first of all i want to know how can i create the image at runtime from the codebehind file in order to represent the image file

     

    Tuesday, December 16, 2008 4:05 PM
  • User-1760427068 posted

    C# or VB?

    Also, I should ask, from where is the image coming?

    Below is some code that might have what you want.  I still need more description to understand the problem.

     

                PictureFileUploadControl.SaveAs(@"C:\foldername\images\" + PictureFileUploadControl.FileName);
                UploadLabel.Text = "File Uploaded: " + PictureFileUploadControl.FileName;
    
                Bitmap m_bmpRepresentation;
                m_bmpRepresentation = new Bitmap(@"C:\foldername\images\" + PictureFileUploadControl.FileName, false);
    
                double theheight = Convert.ToDouble(m_bmpRepresentation.Height);
                double thewidth = Convert.ToDouble(m_bmpRepresentation.Width);
    
                double newheight = (theheight / thewidth) * 100;
     

    Here is a code segment that is part of a program that uploads an image and puts it into a Bitmap so that we can see the height and width of the uploaded image.  As you can see, properties .Height and .Width might be a part of what you want to find. 

    Am I getting closer to what you want to do? 

    Would you like an example (building on this) that would upload an image of your choice and then show it displayed at different sizes (and then you could see the code that made this possible?)

     

    Happy Holidays,

    -Larry

    Tuesday, December 16, 2008 4:11 PM
  • User2057230362 posted

    i preferred if you can supply me an example(in vb.net) also if you can a little bit explain about the: PictureFileUploadControl in your code...

    my purpose is to take an image(a gif) from the client and to upload it and resize it and represent it at the server side.

    my first question is how  i can create an image tag and use it in order to represent the image.

     

    Tuesday, December 16, 2008 4:26 PM
  • User-1760427068 posted
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB_LoadResizePicture.aspx.vb" Inherits="VB_LoadResizePicture" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
    
        <asp:FileUpload id="MyFileUploadControl" Width="400" runat="server" />        
        <asp:Button runat="server" id="AddAttachmentButton" text="Upload" /><br />
        
        <asp:Label ID="UploadLabel" runat="server" Text="Label"></asp:Label><br /><br />
        
        Before Image: <asp:Image ID="BeforeImage" runat="server" /><br /><br />
        
        After Image: <asp:Image ID="AfterImage" runat="server" />
    
        </form>
    </body>
    </html>

     

    Imports System.IO.Path
    Imports System.Drawing  'needed to enable Bitmap
    Imports System.Drawing.Imaging
    
    Partial Class VB_LoadResizePicture
        Inherits System.Web.UI.Page
    
    
    
        Protected Sub AddAttachmentButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddAttachmentButton.Click
    
            If MyFileUploadControl.HasFile Then
    
                Dim newfile As String = MyFileUploadControl.FileName
                MyFileUploadControl.SaveAs(Server.MapPath("~/somefolder/") + newfile)
                UploadLabel.Text = "File Uploaded: " + MyFileUploadControl.FileName
    
                Dim bmp1 As New Bitmap(Server.MapPath("~/somefolder/") + newfile)
                UploadLabel.Text = Server.MapPath("~/somefolder/" + newfile).ToString
    
                Dim bmpheight As Double = Convert.ToDouble(bmp1.Height)
                Dim bmpwidth As Double = Convert.ToDouble(bmp1.Width)
    
                Dim newbmpheight As Integer = Convert.ToInt32(bmpheight / 2)
                Dim newbmpwidth As Integer = Convert.ToInt32(bmpwidth / 2)
    
                bmp1 = bmp1.GetThumbnailImage(newbmpwidth, newbmpheight, Nothing, New IntPtr)
    
                bmp1.Save(Server.MapPath("~/somefolder/thumbnail") + MyFileUploadControl.FileName, ImageFormat.Jpeg)
    
                BeforeImage.ImageUrl = Server.MapPath("~/somefolder/") + newfile
                AfterImage.ImageUrl = Server.MapPath("~/somefolder/thumbnail") + newfile
    
            End If
    
        End Sub
    End Class
     
    Tuesday, December 16, 2008 4:32 PM
  • User2057230362 posted

    I also want that the picture will be saved to the server

    Tuesday, December 16, 2008 4:38 PM
  • User-1760427068 posted
    A picture is uploaded and the computer creates a second file that is half the size of the original. The code is simple and could be modified easily with the math of your chosing. The project is labeled "draft" because after all the work to get the main features to work--the images don't display. They don't turn to red x's either --they are a square with three geometric shapes of three different colors inside. I remember seeing this once before but can't recall the reason. I might think of it tomorrow.
    Tuesday, December 16, 2008 10:53 PM
  • User2057230362 posted

    everything is working except the displaying of the image (before/after)

    Wednesday, December 17, 2008 4:27 AM
  • User-1760427068 posted
     
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB_LoadResizePicture.aspx.vb" Inherits="VB_LoadResizePicture" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
    
        <asp:FileUpload id="MyFileUploadControl" Width="400" runat="server" />        
        <asp:Button runat="server" id="AddAttachmentButton" text="Upload" /><br />
        
        <asp:Label ID="UploadLabel" runat="server" Text="Label"></asp:Label><br /><br />
        
        Before Image: <asp:Image ID="BeforeImage" runat="server" /><br /><br />
        
        After Image: <asp:Image ID="AfterImage" runat="server" /><br /><br />
    
        </form>
    </body>
    </html>

     

    Imports System.IO.Path
    Imports System.Drawing  'needed to enable Bitmap
    Imports System.Drawing.Imaging
    
    Partial Class VB_LoadResizePicture
        Inherits System.Web.UI.Page
    
    
    
        Protected Sub AddAttachmentButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddAttachmentButton.Click
    
            If MyFileUploadControl.HasFile Then
    
                Dim newfile As String = MyFileUploadControl.FileName
                MyFileUploadControl.SaveAs(Server.MapPath("~/somefolder/") + newfile)
                UploadLabel.Text = "File Uploaded: " + MyFileUploadControl.FileName
    
                Dim bmp1 As New Bitmap(Server.MapPath("~/somefolder/") + newfile)
                UploadLabel.Text = Server.MapPath("~/somefolder/" + newfile).ToString
    
                Dim bmpheight As Double = Convert.ToDouble(bmp1.Height)
                Dim bmpwidth As Double = Convert.ToDouble(bmp1.Width)
    
                Dim newbmpheight As Integer = Convert.ToInt32(bmpheight / 4)
                Dim newbmpwidth As Integer = Convert.ToInt32(bmpwidth / 4)
    
                bmp1 = bmp1.GetThumbnailImage(newbmpwidth, newbmpheight, Nothing, New IntPtr)
    
                bmp1.Save(Server.MapPath("~/somefolder/thumbnail") + MyFileUploadControl.FileName, ImageFormat.Jpeg)
    
                BeforeImage.ImageUrl = "somefolder/" + newfile
                AfterImage.ImageUrl = "somefolder/thumbnail" + newfile
    
    
            End If
    
        End Sub
    
    End Class
    

     

    ***************************************

    Did you want the large image to be deleted or would you like to keep it?

    -Larry

    Wednesday, December 17, 2008 10:06 AM
  • User2057230362 posted

    i will want it to be deleted .

     

    Wednesday, December 17, 2008 12:39 PM
  • User-1760427068 posted

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB_LoadResizePicture.aspx.vb" Inherits="VB_LoadResizePicture" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
    
        <asp:FileUpload id="MyFileUploadControl" Width="400" runat="server" />        
        <asp:Button runat="server" id="AddAttachmentButton" text="Upload" /><br />
        
        Thumbnail: <asp:Image ID="MyThumbnail" runat="server" /><br /><br />
    
        </form>
    </body>
    </html>

      

    Imports System.IO 'needed to enable .Delete
    Imports System.IO.Path
    Imports System.Drawing  'needed to enable Bitmap
    Imports System.Drawing.Imaging
    
    Partial Class VB_LoadResizePicture
        Inherits System.Web.UI.Page
    
    
    
        Protected Sub AddAttachmentButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddAttachmentButton.Click
    
            If MyFileUploadControl.HasFile Then
    
                Dim newfile As String = MyFileUploadControl.FileName
                MyFileUploadControl.SaveAs(Server.MapPath("~/somefolder/original") + newfile)
    
                Dim bmp1 As New Bitmap(Server.MapPath("~/somefolder/original") + newfile)
    
                Dim bmpheight As Double = Convert.ToDouble(bmp1.Height)
                Dim bmpwidth As Double = Convert.ToDouble(bmp1.Width)
    
                Dim newbmpheight As Integer = Convert.ToInt32(bmpheight / 4)
                Dim newbmpwidth As Integer = Convert.ToInt32(bmpwidth / 4)
    
                Dim bmp2 As Bitmap = bmp1.GetThumbnailImage(newbmpwidth, newbmpheight, Nothing, New IntPtr)
    
                bmp2.Save(Server.MapPath("~/somefolder/") + MyFileUploadControl.FileName, ImageFormat.Jpeg)
    
                MyThumbnail.ImageUrl = "somefolder/" + newfile
    
                bmp1.Dispose()
    
                Dim TheFile As FileInfo = New FileInfo(Server.MapPath("~/somefolder/original") + newfile)
                If TheFile.Exists Then
    
                    File.Delete(Server.MapPath("~/somefolder/original") + newfile)
    
                End If
    
            End If
    
        End Sub
    
    End Class
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 17, 2008 2:15 PM
  • User-1760427068 posted

    A segment was added to delete the original graphic and a Dispose was added so that the file could be deleted without an error code (the error would be because the program was "hanging on" to the file that you want to delete).

    Additionally, the naming structure is changed.

    The name of the graphic file goes on the file that was previously called "thumbnail" + filename.

    The original graphic is given the name "original" + filename since we won't be keeping it.

    -Larry

    Wednesday, December 17, 2008 2:18 PM