Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: purocuque en 28 Abril 2015, 01:59 am



Título: Ayuda en c# con poner imagen sobre imagen
Publicado por: purocuque en 28 Abril 2015, 01:59 am
Buen día quiero empezar un programa en c# orientado a objetos en el que me permita seleccionar una imagen e insertarla dentro de otra que haya abierto anteriormenete que la imagen que abra me permita cambiar tamaño
Dejaré un ejemplo por aca. Básicamente lo que necesito es insertar una imagen sobre otra :-X (http://www.lawebdelprogramador.com/usr/185000/185789/553ec8ac2dda6-Sin-titulo.jpg)


Título: Re: Ayuda en c# con poner imagen sobre imagen
Publicado por: USLO en 30 Abril 2015, 13:12 pm

Ni tan siquiera te has molestado en preguntar lo que necesitas.
Y quieres que te lo hagamos o que?

Voy a abrir un tema nuevo(Necesito dinero).Sin preguntas a ver si cae del cielo.

En fin.Hay que molestarse un poco mas en formular preguntas y mostrar interés en resolver sus propias dudas.

Ahora si sigues interesado formula tu pregunta?
Un saludo.


Título: Re: Ayuda en c# con poner imagen sobre imagen
Publicado por: Eleкtro en 30 Abril 2015, 18:34 pm
En un principio yo también veo mal ofrecer ayuda a quien lo pide todo hecho sin mostrar código alguno, pero me he dado cuenta de que un "ejercicio" o utilidad cómo esta me faltaba en mi librería personal de códigos y me ha llamado la atención el desarrollar este tipo de utilidad para un uso genérico, ya que lo hago pues lo comparto.

Imagen trasera e imagen delantera
(http://i.imgur.com/JoStS4Y.jpg)     (http://i.imgur.com/WHZz9D0.png)

Superposición (opaca)
(http://i.imgur.com/xChpBRC.jpg)

Código fuente
Código
  1.    ''' <remarks>
  2.    ''' *****************************************************************
  3.    ''' Snippet Title: Overlay Images
  4.    ''' Code's author: Elektro
  5.    ''' Date Modified: 30-April-2015
  6.    ''' *****************************************************************
  7.    ''' </remarks>
  8.    ''' <summary>
  9.    ''' Overlay an image over a background image.
  10.    ''' </summary>
  11.    ''' <param name="backImage">The background image.</param>
  12.    ''' <param name="topImage">The topmost image.</param>
  13.    ''' <param name="topPosX">An optional adjustment of the top image's "X" position.</param>
  14.    ''' <param name="topPosY">An optional adjustment of the top image's "Y" position.</param>
  15.    ''' <returns>The overlayed image.</returns>
  16.    ''' <exception cref="ArgumentNullException">backImage or topImage</exception>
  17.    ''' <exception cref="ArgumentException">Image bounds are greater than background image.;topImage</exception>
  18.    Public Shared Function OverlayImages(ByVal backImage As Image,
  19.                                         ByVal topImage As Image,
  20.                                         Optional ByVal topPosX As Integer = 0,
  21.                                         Optional ByVal topPosY As Integer = 0) As Image
  22.  
  23.        If backImage Is Nothing Then
  24.            Throw New ArgumentNullException(paramName:="backImage")
  25.  
  26.        ElseIf topImage Is Nothing Then
  27.            Throw New ArgumentNullException(paramName:="topImage")
  28.  
  29.        ElseIf (topImage.Width > backImage.Width) OrElse
  30.               (topImage.Height > backImage.Height) Then
  31.            Throw New ArgumentException(message:="Image bounds are greater than background image.", paramName:="topImage")
  32.  
  33.        Else
  34.            topPosX += CInt((backImage.Width / 2) - (topImage.Width / 2))
  35.            topPosY += CInt((backImage.Height / 2) - (topImage.Height / 2))
  36.  
  37.            Dim bmp As New Bitmap(backImage.Width, backImage.Height)
  38.  
  39.            Using canvas As Graphics = Graphics.FromImage(bmp)
  40.  
  41.                canvas.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  42.  
  43.                canvas.DrawImage(image:=backImage,
  44.                                 destRect:=New Rectangle(0, 0, bmp.Width, bmp.Height),
  45.                                 srcRect:=New Rectangle(0, 0, bmp.Width, bmp.Height),
  46.                                 srcUnit:=GraphicsUnit.Pixel)
  47.  
  48.                canvas.DrawImage(image:=topImage,
  49.                                 destRect:=New Rectangle(topPosX, topPosY, topImage.Width, topImage.Height),
  50.                                 srcRect:=New Rectangle(0, 0, topImage.Width, topImage.Height),
  51.                                 srcUnit:=GraphicsUnit.Pixel)
  52.  
  53.                canvas.Save()
  54.  
  55.            End Using
  56.  
  57.            Return bmp
  58.  
  59.        End If
  60.  
  61.    End Function

Traducción online a C#
Código
  1. /// <remarks>
  2. /// *****************************************************************
  3. /// Snippet Title: Overlay Images
  4. /// Code's author: Elektro
  5. /// Date Modified: 30-April-2015
  6. /// *****************************************************************
  7. /// </remarks>
  8. /// <summary>
  9. /// Overlay an image over a background image.
  10. /// </summary>
  11. /// <param name="backImage">The background image.</param>
  12. /// <param name="topImage">The topmost image.</param>
  13. /// <param name="topPosX">An optional adjustment of the top image's "X" position.</param>
  14. /// <param name="topPosY">An optional adjustment of the top image's "Y" position.</param>
  15. /// <returns>The overlayed image.</returns>
  16. /// <exception cref="ArgumentNullException">backImage or topImage</exception>
  17. /// <exception cref="ArgumentException">Image bounds are greater than background image.;topImage</exception>
  18. public static Image OverlayImages(Image backImage, Image topImage, int topPosX = 0, int topPosY = 0)
  19. {
  20.  
  21.        if (backImage == null) {
  22. throw new ArgumentNullException(paramName: "backImage");
  23.  
  24. } else if (topImage == null) {
  25. throw new ArgumentNullException(paramName: "topImage");
  26.  
  27. } else if ((topImage.Width > backImage.Width) || (topImage.Height > backImage.Height)) {
  28. throw new ArgumentException("Image bounds are greater than background image.", "topImage");
  29.  
  30. } else {
  31. topPosX += Convert.ToInt32((backImage.Width / 2) - (topImage.Width / 2));
  32. topPosY += Convert.ToInt32((backImage.Height / 2) - (topImage.Height / 2));
  33.  
  34. Bitmap bmp = new Bitmap(backImage.Width, backImage.Height);
  35.  
  36. using (Graphics canvas = Graphics.FromImage(bmp)) {
  37. canvas.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;
  38. canvas.DrawImage(image: backImage, destRect: new Rectangle(0, 0, bmp.Width, bmp.Height), srcRect: new Rectangle(0, 0, bmp.Width, bmp.Height), srcUnit: GraphicsUnit.Pixel);
  39. canvas.DrawImage(image: topImage, destRect: new Rectangle(topPosX, topPosY, topImage.Width, topImage.Height), srcRect: new Rectangle(0, 0, topImage.Width, topImage.Height), srcUnit: GraphicsUnit.Pixel);
  40.  
  41. canvas.Save();
  42. }
  43. return bmp;
  44. }
  45. }
  46.  
  47. //=======================================================
  48. //Service provided by Telerik (www.telerik.com)
  49. //=======================================================

Modo de empleo (el mismo que he usado para generar el ejemplo de superposición de arriba)
Código
  1. Dim backImg As Image = Image.FromFile("C:\back.jpg")
  2. Dim topImg As Image = Image.FromFile("C:\top.png")
  3. Dim overlay As Image = OverlayImages(backImg, topImg, topPosX:=+5, topPosY:=-15)
  4. overlay.Save("C:\Overlay.jpg", Imaging.ImageFormat.Jpeg)
  5. Process.Start("C:\Overlay.jpg")

Saludos


Título: Re: Ayuda en c# con poner imagen sobre imagen
Publicado por: USLO en 4 Mayo 2015, 09:07 am
Que bueno eres Eleкtro.

Ya que te lo dan mascado, trata de entenderlo(Purocuque).
Si no, no aprendes.

Un saludo.