• Publicidad

Mostrar imagenes en un gridview con C#

Todo lo relacionado con lenguajes de programación distintos de Perl: PHP, Java, C++, Ruby, Python, etc.

Mostrar imagenes en un gridview con C#

Notapor ricky_jr » 2009-12-08 18:22 @807

Bueno, no sé si será el lugar correcto pero ya me harté de buscar y este foro en el pasado me ayudó mucho.

Mi problema es con c# en .Net es muy interesante: tengo una tabla en SQL 2005, contiene imágenes y quiero que se vean en un gridview; como ven en el código, está de la patada.

Bueno el problema radica principalmente en que no puede convertir el tipo de dato y almacenarlo en un array de tipo byte[]; ahí está el error.

Sintáxis: [ Descargar ] [ Ocultar ]
Using c Syntax Highlighting
  1.         public void Page_Load()
  2.         {
  3.             if (!IsPostBack)
  4.             {
  5.                 loadContraportada();
  6.             }
  7.         }
  8.         public void loadContraportada()
  9.         {
  10.             Regex objIsNumeric = new Regex("(^-?\\d\\d*$)");
  11.  
  12.             if (Request.QueryString["Id"].Length <= 0)
  13.             {
  14.                 return;
  15.             }
  16.             int IntWidth = 30;
  17.             int IntHeight = 30;
  18.  
  19.             Utilerias.Acceso UAc = new Utilerias.Acceso();
  20.             String SQLQuery = "";
  21.             SQLQuery = "select " + Request.QueryString["campo"] + " from tb_Skin_Portal where IdSkin=" + Request.QueryString["Id"];
  22.  
  23.             DataTable objdt = new DataTable();
  24.             objdt = UAc.GetDataTableBD(SQLQuery, "moebius_db", false);
  25.  
  26.             if (objdt.Rows.Count > 0)
  27.             {
  28.  
  29.                 DataRow r = objdt.Rows[0];
  30.  
  31.                 //bytImg = null;
  32.  
  33.          
  34.  
  35.                 byte[] bytImg;
  36.                 bytImg = Encoding.UTF8.GetBytes(r[Request.QueryString["campo"]].ToString());
  37.  
  38.  
  39.                 Bitmap oBitmap = new Bitmap(IntWidth, IntHeight);
  40.                 oBitmap = Convertir_a_Image(bytImg);
  41.  
  42.                 (oBitmap, IntWidth, IntHeight));
  43.                 Bitmap bmpOut = oBitmap;
  44.  
  45.                 Response.ContentType = "image/jpeg";
  46.                 bmpOut.Save(Response.OutputStream, ImageFormat.Jpeg);
  47.             }
  48.          }
  49.         private Bitmap CreateThumbNail(Bitmap postedFile, int width, int height)
  50.         {
  51.             System.Drawing.Bitmap bmpOut = default(System.Drawing.Bitmap);
  52.             ImageFormat Format = postedFile.RawFormat;
  53.  
  54.             Decimal Ratio = 0;
  55.             int NewWidth = 0;
  56.             int NewHeight = 0;
  57.  
  58.             //*** If the image is smaller than a thumbnail just return it
  59.             if ((postedFile.Width < width) && (postedFile.Height < height))
  60.             {
  61.                 return postedFile;
  62.             }
  63.  
  64.             if (postedFile.Width > postedFile.Height)
  65.             {
  66.                 Ratio = Convert.ToDecimal(width / postedFile.Width);
  67.                 NewWidth = width;
  68.                 Decimal Temp = postedFile.Height * Ratio;
  69.                 NewHeight = Convert.ToInt32(Temp);
  70.             }
  71.             else
  72.             {
  73.                 Ratio = Convert.ToDecimal(height / postedFile.Height);
  74.                 NewHeight = height;
  75.                 Decimal Temp = postedFile.Width * Ratio;
  76.                 NewWidth = Convert.ToInt32(Temp);
  77.             }
  78.  
  79.             bmpOut = new Bitmap(NewWidth, NewHeight);
  80.             Graphics g = Graphics.FromImage(bmpOut);
  81.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  82.             g.FillRectangle(Brushes.White, 0, 0, NewWidth, NewHeight);
  83.             g.DrawImage(postedFile, 0, 0, NewWidth, NewHeight);
  84.             postedFile.Dispose();
  85.             return bmpOut;
  86.         }
  87.  
  88.         public Bitmap Convertir_a_Image(byte[] imagen)
  89.         {
  90.             MemoryStream ms = new MemoryStream(imagen, 0, imagen.Length, true);
  91.             Bitmap bmp = new Bitmap(ms);
  92.             ms.Close();
  93.             return bmp;
  94.         }
  95.     }
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4
ricky_jr
Perlero nuevo
Perlero nuevo
 
Mensajes: 13
Registrado: 2009-08-12 21:07 @921

Publicidad

Volver a Programación en general

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 0 invitados